【问题标题】:Rethrow from a function in the catch block从 catch 块中的函数重新抛出
【发布时间】:2014-10-26 23:20:50
【问题描述】:

在这种情况下,我想根据抛出的异常执行一些操作,然后重新抛出异常。是否推荐 - 我的目标是根据抛出的异常做一些工作并重新抛出它,让应用程序崩溃并生成在异常中包含调用堆栈的转储。

class Foo
{
public:

void HandleException(const std::exception& ex)
{
    // Log, report some metrics
    throw;
}

void Work(//Some inputs)
{
     try
     {
          // trying doing some work
     }
     catch (const std::exception& ex)
     {
          // This is really an exceptional situation, and the exception should be thrown which  
          // cause the exe to abort and create dump.
          // Intention is to preserve call stack and have it in dump.
          HandleException(ex);
     }
}
}

让我为这个问题添加另一个注释:当我将 HandleException 作为 lambda 函数时,抛出 lambda 会导致异常。我需要捕获一些状态吗?我该怎么做?

【问题讨论】:

    标签: c++ exception-handling


    【解决方案1】:

    当您捕获异常时,您有两种选择:

    • 以某种方式实现最初的目标(合同),例如通过重试。
    • 通过投掷报告失败。

    重新抛出原始异常是实现第二个要点的一种方法。

    【讨论】:

      猜你喜欢
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 2012-09-14
      • 2017-06-29
      • 2011-10-24
      • 1970-01-01
      相关资源
      最近更新 更多