65,210
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
class Base
{
int i;
public:
class BaseExcept{};
Base(int ii):i(ii){throw BaseExcept();}//成员对象初始化时抛出了异常。
};
class Derived:public Base
{
public:
class DerivedExcept
{
const char* msg;
public:
DerivedExcept(const char* message):msg(message){}
const char* what()const{return msg;}
};
Derived(int j)try:Base(j)
{
cout<<"This won't print"<<endl;
}
catch(BaseExcept&)
{
throw DerivedExcept("Base subobject threw");;
}
};
int main()
{
try
{
Derived d(3);
}
catch(Derived::DerivedExcept& d)
{
cout<<d.what()<<endl;
}
return 0;
}Base subobject threw#include<iostream>
using namespace std;
class Base
{
int i;
public:
class BaseExcept{};
Base(int ii):i(ii){throw BaseExcept();}//成员对象初始化时抛出了异常。
};
class Derived:public Base
{
public:
class DerivedExcept
{
const char* msg;
public:
DerivedExcept(const char* message):msg(message){}
const char* what()const{return msg;}
};
Derived(int j)try:Base(j)
{
cout<<"This won't print"<<endl;
}
catch(BaseExcept&)
{
throw DerivedExcept("Base subobject threw");;
}
};
int main()
{
try
{
Derived d(3);
}
catch(Derived::DerivedExcept& d)
{
cout<<d.what()<<endl;
}
system("pause");
return 0;
}
===========================
Base subobject threw
请按任意键继续. . .
dev-c++