c++ - 在析构函数中正确使用 std::uncaught_exception

标签 c++ exception

有些文章的结论是“永远不要从析构函数中抛出异常”,“std::uncaught_exception() 没有用”,例如:

不过我好像没听懂。所以我写了一个小测试示例(见下文)。

由于测试示例一切正常,我非常感谢您提出有关它可能有什么问题的评论?

测试结果:

./主要

    Foo::~Foo(): caught exception - but have pending exception - ignoring
    int main(int, char**): caught exception: from int Foo::bar(int)

./main 1

    Foo::~Foo(): caught exception -  but *no* exception is pending - rethrowing
    int main(int, char**): caught exception: from Foo::~Foo()

example:

// file main.cpp
// build with e.g. "make main"
// tested successfully on Ubuntu-Karmic with g++ v4.4.1
#include <iostream>

class Foo {
  public:

  int bar(int i) {
    if (0 == i)
      throw(std::string("from ") + __PRETTY_FUNCTION__);
    else
      return i+1;
  }

  ~Foo() {
    bool exc_pending=std::uncaught_exception();
    try {
      bar(0);
    } catch (const std::string &e) {
      // ensure that no new exception has been created in the meantime
      if (std::uncaught_exception()) exc_pending = true;

      if (exc_pending) {
        std::cerr << __PRETTY_FUNCTION__ 
                  << ": caught exception - but have pending exception - ignoring"
                  << std::endl;
      } else {
        std::cerr << __PRETTY_FUNCTION__
                  << ": caught exception -  but *no* exception is pending - rethrowing"
                  << std::endl;
        throw(std::string("from ") + __PRETTY_FUNCTION__);
      }
    }
  }

};

int main(int argc, char** argv) {
  try {
    Foo f;
    // will throw an exception in Foo::bar() if no arguments given. Otherwise
    // an exception from Foo::~Foo() is thrown.
    f.bar(argc-1);
  } catch (const std::string &e) {
    std::cerr << __PRETTY_FUNCTION__ << ": caught exception: " << e << std::endl;
  }
  return 0;
}

已添加:换句话说:尽管某些文章中有警告,但它按预期工作 - 那么它可能有什么问题?

最佳答案

Herb Sutter 指的是另一个问题。他在谈论:

try
{
}
catch (...)
{
    try
    {
        // here, std::uncaught_exception() will return true
        // but it is still safe to throw an exception because
        // we have opened a new try block
    }
    catch (...)
    {
    }
}

所以问题是,如果 std::uncaught_exception() 返回 true,您不确定是否可以安全地抛出异常。为了安全起见,您最终不得不避免在 std::uncaught_exception() 返回 true 时抛出异常。

关于c++ - 在析构函数中正确使用 std::uncaught_exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2475138/

相关文章:

java - 检查 boolean 状态时是否有更干净的方法来处理 try/catch?

c++ - integral_constant 与 constexpr

c++ - 参数评估与链式方法之间是否存在有保证的“先发生”关系?

c++ - 如何在给定特定节点的 BST 中找到直接较大的元素?

android - fragment 抛出时在 Activity 中捕获异常

java - Android正在尝试使用回收图像

c++ - 转换大小写 ASCII 字符

c++ - 用C++重写一个简单的Pygame 2D绘图函数

javafx组合框事件监听器异常

exception - 无效日期异常 |索拉尔