// 构造函数失败处理
#include <iostream>
using namespace std;
class CFoo {
public:
CFoo()
{
cout << "construct CFoo." << endl;
// an error occurred
throw -1;
}
~CFoo()
{
cout << "destruct CFoo." << endl;
}
};
int main (int argc, char *argv[])
{
CFoo *Foo = NULL;
try
{
Foo = new CFoo;
}
catch (...)
{
cout << "catch exception." << endl;
// Foo == NULL, can't delete it
}
getch();
return 0;
}
#include <iostream>
using namespace std;
class CFoo {
public:
CFoo()
{
cout << "construct CFoo." << endl;
// an error occurred
throw -1;
}
~CFoo()
{
cout << "destruct CFoo." << endl;
}
};
int main (int argc, char *argv[])
{
CFoo *Foo = NULL;
try
{
Foo = new CFoo;
}
catch (...)
{
cout << "catch exception." << endl;
// Foo == NULL, can't delete it
}
getch();
return 0;
}