在构造函数中引发的异常的处理程序是什么? [英] What is the handler of an exception raised in constructor?

查看:131
本文介绍了在构造函数中引发的异常的处理程序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C ++异常处理。我想的规则是


  1. 如果在调用链中找不到异常处理程序

  2. 处理程序是 catch {}

但是,我无法理解以下行为。

  #include< iostream> 
#include< exception>
using namespace std;

struct X {
X()try {throw exception(); }
catch(exception& e){
cout<< 在构造函数中捕获异常! << endl
}
};

int main(){

try {
throw exception();
}
catch(exception& e){
cout<< 异常捕获在函数中。 << endl
}
cout<< 在异常被捕获函数<< endl

try {
X x;
}
catch(exception& e){
cout<< 为什么异常被抓住了! << endl
}

return 0;
}

输出为



在函数中捕获异常。
在函数中捕获异常之后
在构造函数中捕获异常!
为什么会再次捕获异常!



问题1: X的构造函数被捕获(或处理)两次。或者为什么catch {}块在构造函数之后不会被视为构造函数中的异常的处理程序?



如果我不将 X x; 放在 try {} 阻塞并捕获它在 main()中,输出为:



在函数中捕获异常。
在函数中捕获异常之后
在构造函数中捕获异常!
在抛出'std :: exception'实例后调用终止
what():std :: exception
中止(内核转储)

$问题2:
当我们没有 X x; 时调用默认的terminate()函数a try block?

解决方案

两个例外。

  struct X {
X()try {throw exception(); }
catch(exception& e){
cout<< 在构造函数中捕获异常! << endl
}
};

异常后,您将在构造函数中处理它。 ,您没有构建
对象,并且您在调用方没有对象。调用者
应该处理未构造的对象情况。



第二个问题,是。按标准中的 [except.terminate] 。它会调用 std :: terminate


I am new to C++ exception handling. The rule in my mind is that,

  1. If no handler of an exception is found in the call chain (the stack of function calling), then the terminate function is called.
  2. A handler is a catch {} block.

However, I cannot understand the behaviour of the following.

#include <iostream>
#include <exception>
using namespace std;

struct X {
  X() try { throw exception(); }
  catch (exception &e) {
    cout << "Exception caught in constructor!" << endl;
  }
};

int main() {

  try {
    throw exception();
  }
  catch (exception &e) {
    cout << "Exception caught in function." << endl;
  }
  cout << "After Exception being caught in function" << endl;

  try {
    X x;
  }
  catch (exception &e) {
    cout << "Why exception is caught again!" << endl;
  }

  return 0;
}

The output is

Exception caught in function. After Exception being caught in function Exception caught in constructor! Why exception is caught again!

Question 1: It seems that the exception thrown in the constructor of X is caught (or. handled) twice. Or why the catch{} block following the constructor not counted as a handler for the exception in the constructor?

If I do not put X x; in the try{} block and catch it in the main(), the output is:

Exception caught in function. After Exception being caught in function Exception caught in constructor! terminate called after throwing an instance of 'std::exception' what(): std::exception Aborted (core dumped)

Question 2: Is the default terminate() function called when we do not have X x; in a try block?

解决方案

You have two exceptions.

struct X {
  X() try { throw exception(); }
  catch (exception &e) {
    cout << "Exception caught in constructor!" << endl;
  }
};

After an exception you will handle it in the constructor. BUT you didn't construct the object yet and you have no object in the caller's side. The caller should handle an un-constructed object situation.

Second question, yes. Per [except.terminate] in the standard. It causes to invoke std::terminate.

这篇关于在构造函数中引发的异常的处理程序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆