相关疑难解决方法(0)

在析构函数中正确使用std :: uncaught_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()

例:

// 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) …
Run Code Online (Sandbox Code Playgroud)

c++ exception

8
推荐指数
2
解决办法
2232
查看次数

ISO c ++ 15.3.10:为什么这种未定义的行为?

从相关的c ++标准部分:

引用构造函数的函数try-block的处理程序中的对象的任何非静态成员或基类,或该对象的析构函数导致未定义的行为.

例如.

T::~T() 
{
      try {
        this->nonstatic_member; // iff I read the quote correctly
      } catch( ... ) {
      }
}
Run Code Online (Sandbox Code Playgroud)

那为什么这个未定义的行为?

c++ language-lawyer c++11

4
推荐指数
2
解决办法
259
查看次数

标签 统计

c++ ×2

c++11 ×1

exception ×1

language-lawyer ×1