本文介绍了异常的生命周期是否受嵌套处理程序的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码片段:

struct ExceptionBase : virtual std::exception{};
struct SomeSpecificError : virtual ExceptionBase{};
struct SomeOtherError : virtual ExceptionBase{};

void MightThrow();
void HandleException();
void ReportError();

int main()
{
  try
  {
    MightThrow();
  }
  catch( ... )
  {
    HandleException();
  }
}

void MightThrow()
{
  throw SomeSpecificError();
}

void HandleException()
{
  try
  {
    throw;
  }
  catch( ExceptionBase const & )
  {
    // common error processing
  }

  try
  {
    throw;
  }
  catch( SomeSpecificError const & )
  {
    // specific error processing
  }
  catch( SomeOtherError const & )
  {
    // other error processing
  }

  ReportError();
}

void ReportError()
{
}


b $ b

标准中的第15.1.4节告诉我们:

Section 15.1.4 from the standard tells us:

我在正在查看 main 中的处理程序作为最后处理程序?因此,在 HandleException 中允许任何数量的rethrow和catch,而不会导致破坏当前异常对象?

Am I correct in viewing the handler in main as the "last handler?" And therefore any number of rethrow's and catches are allowed in HandleException without causing the destruction of the current exception object?

推荐答案

感谢您发布的评论和回答。我没有看到我在找什么,所以我要添加一些信息从 aschepler提供的答案到我的后续问题。

Thanks for the comments and answers posted thus far. I haven't seen quite what I'm looking for so I'm going to add some information from an answer provided by aschepler to my follow-up question.

15.3p8:最近激活的处理程序仍处于活动状态的异常称为当前处理的异常。

15.3p8: The exception with the most recently activated handler that is still active is called the currently handled exception.

我认为标准的语言很清楚, main 实际上是最后处理程序。因此,异常的生命周期不受嵌套处理程序的影响。

I think the language of the standard is quite clear here and that main is in fact the last handler. Therefore the lifetime of an exception is not affected by nested handlers.

这篇关于异常的生命周期是否受嵌套处理程序的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 01:34