异常中止是使用function-try-block方法在derive类中处理的

时间:2015-07-06 09:00:43

标签: c++ inheritance exception-handling

在下面的代码片段中,从Base类的构造函数程序抛出异常时被中止。我相信派生类已经正确处理了异常但仍然程序被终止并且没有发生优雅的程序退出。你能不能让我知道这种方法有什么问题,就好像在main函数中处理异常一样,在派生类对象p上强加try catch块然后rogramme不会被终止。

#include <iostream>
#include <memory>
using namespace std;
class A
{
  int *memory;
  public:
  A()
  {
   cout<<"calling constructor class A\n";
   memory= new int [100000000000000];

  }
  void show()
  {
  cout<<"show A\n";
  }
  ~A()
  {
    cout<<"calling destructor class A\n";
  }
};

class B : public A
{
  public:
 B()

  try :A() {
  cout<<"calling B's constructor\n";
  }
  catch(...)
  {
  cout << "exception occurred\n";
  }
  ~B()
  {
    cout<<"calling B's destructor\n";
  }


};

int main()
{
B obj;
obj.show();
return 1;
}

0 个答案:

没有答案