}
void Other() {}
protected:
string m_name;
};
class MyTest_Parts
{
public:
MyTest_Parts ()
{
cout << "构造一个MyTest_Parts类型的对象" << endl;
}
virtual ~ MyTest_Parts ()
{
cout << "销毁一个MyTest_Parts类型的对象"<< endl;
}
};
class MyTest_Derive : public MyTest_Base
{
public:
MyTest_Derive (string name = "") : m_component(), MyTest_Base(name)
{
throw std::exception("在MyTest_Derive对象的构造函数中抛出了一个异常!");
cout << "构造一个MyTest_Derive类型的对象,对象名为:"<<m_name << endl;
}
virtual ~ MyTest_Derive ()
{
cout << "销毁一个MyTest_Derive类型的对象,对象名为:"<<m_name << endl;
}
protected:
MyTest_Parts m_component;
};
void main()
{
try
{
// 对象构造时将会抛出异常
MyTest_Derive obj1("obj1");
obj1.Func();
obj1.Other();
}
catch(std::exception e)
{
cout << e.what() << endl;
}
catch(...)
{
cout << "unknow exception"<< endl;
}
}
程序运行的结果是:
构造一个MyTest_Base类型的对象,对象名为:obj1
构造一个MyTest_Parts类型的对象
销毁一个MyTest_Parts类型的对象