代码如下:
#include
class TException
{
public:
TException()
{
std::cout << "hello,world!" << std::endl;
}
};
double Func(double x, double y)
{
if (0 == y)
{
throw new TException();
}
}
int WrapFunc()
{
Func(0, 0);
return 0;
}
int main()
{
try
{
WrapFunc();
}
catch (TException* e)
{
std::cout << "catch exception" << std::endl;
}
catch (...)//捕获所有的异常
{
std::cout << "catch all exception" << std::endl;
}
return 0;
}
分析
当抛出new TException,catch语句必须是定义为指针,当抛出TException()对象的时候,catch语句必须是对象可以是catch(TException& e)或者catch(const TException& e)