代碼如下:
#include <iostream>
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)