举例:
#include<iostream>
using namespace std;
class MyException
{
public:
int a;
MyException(int a);
virtual void SayHello(int a) noexcept {}
};
MyException::MyException(int a)
{
this->a = a;
}
int func(int x) noexcept
{
if (x == 0)
{
return 0;
}
else if (x == 1)
{
throw "helloworld";
}
else if (x == 2)
{
throw new MyException(2);
}
}
int main()
{
try
{
func(2);
}
catch (int a)
{
cout << a << endl;
}
catch (char* str)
{
cout << str << endl;
}
catch (MyException* p)
{
cout << p->a << endl;
}
getchar();
return 0;
}
func函数声明了无异常,noexcept,但是最后调用的时候func(2)之后,实际抛出了异常。此时程序终止在这里:throw new MyException(2);
验证了:
https://www.jianshu.com/p/08a53d8c9670