You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
exception/user_defined_exception_clas...

44 lines
660 B

#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();
}
}