parent
de0ca8015a
commit
bed9451f47
@ -0,0 +1,44 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
|
using std::cin, std::cout, std::string;
|
||||||
|
|
||||||
|
class ArrayException
|
||||||
|
{
|
||||||
|
const char *info;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ArrayException(const char *what) : info{what}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
void print() const
|
||||||
|
{
|
||||||
|
std::cout << info << '\n';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class IntArray
|
||||||
|
{
|
||||||
|
int data[10];
|
||||||
|
|
||||||
|
public:
|
||||||
|
int &operator[](int index)
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= 10)
|
||||||
|
throw ArrayException{"下标越界"};
|
||||||
|
return data[index];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
IntArray array{};
|
||||||
|
array[10] = 5;
|
||||||
|
}
|
||||||
|
catch (const ArrayException &exception)
|
||||||
|
{
|
||||||
|
exception.print();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue