#include<iostream>
using namespace std;
class test
{
public:
test()
{
cout<<"hello";}
~test()
{
cout<<"hi";
throw "const";
}
void display()
{
cout<<"faq";
}
};
int main()
{
test t;
try{
}
catch(char const *e)
{
cout<<e;
}
t.display();
}
输出:如何处理异常?
我知道从析构函数抛出例外,我正在违反基本的C++的法律但我仍想知道的是他们的任何方式异常可以被处理。
try
{
test t;
t.Display();
}
和完整版: - catch
块 - t
的
#include<iostream>
using namespace std;
class test
{
public:
test()
{
cout << "hello" << endl;
}
~test()
{
cout << "hi" << endl;
throw "const";
}
void display()
{
cout << "faq" << endl;
}
};
int main()
{
try
{
test t;
t.display();
}
catch(char const *e)
{
cout << e << endl;
}
}
所以,除了这个小例子之外,你真正想要完成什么?因为这感觉就像你做错了... – Chad