#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