參見https://www.runoob.com/cplusplus/cpp-exceptions-handling.html
C++ 標准的異常
C++ 提供了一系列標准的異常,定義在
下表是對上面層次結構中出現的每個異常的說明:
自定義異常
#include <iostream>
#include <exception>
#include <string>
using namespace std;
class MyException : public exception
{
public:
MyException() : message("Error."){}
MyException(string str) : message("Error : " + str) {}
~MyException() throw () {
}
virtual const char* what() const throw () {
return message.c_str();
}
private:
string message;
};
int main()
{
try
{
throw MyException();
}
catch(MyException& e)
{
std::cout << e.what() << std::flush;
}
catch(std::exception& e)
{
//其他的錯誤
}
}