1-12-2 创建自己的异常类

1.12-2 创建自己的异常类

#include <iostream>
<!--more-->
#include <exception>
using namespace std;
class MyClass
{
class Myexception : public logic_error
{
public:
Myexception(const char *s) : logic_error(s) {}
};

public:
void ThrowExcept() throw(Myexception)
{
throw Myexception("my exception");
}
};

int main()
{
try
{
MyClass mc;
mc.ThrowExcept();
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
}
}