【发布时间】:2017-01-08 13:14:42
【问题描述】:
如果我传递派生类的对象,那么应该调用派生类的 catch 块。但是输出表明异常被基类捕获。为什么?
#include<iostream>
using namespace std;
class Base {};
class Derived: public Base {};
int main()
{
Derived d;
// some other stuff
try {
// Some monitored code
throw d;
}
catch(Base b) {
cout<<"Caught Base Exception";
}
catch(Derived d) { //This catch block is NEVER executed
cout<<"Caught Derived Exception";
}
getchar();
return 0;
}
【问题讨论】:
-
你应该移动带有派生大小写的catch,然后是基数。它们没有超载。
-
不是骗子,
catch子句不同。 -
是的,这是对第二个问题的欺骗,在我发表评论时尚未链接到该问题。
标签: c++ inheritance exception-handling