正确使用std :: uncaught_exception在析构函数中 [英] right usage of std::uncaught_exception in a destructor

查看:159
本文介绍了正确使用std :: uncaught_exception在析构函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些文章总结为从析构函数不会抛出异常,而std :: uncaught_exception()是无用的,例如:





但似乎我没有得到答案。所以我写了一个小测试的例子(见下文)。



由于一切都很好的测试示例,我会非常感谢一些评论,可能是错误的? / p>

测试结果:



./ main

 
Foo ::〜Foo():捕获异常 - 但有待处理的异常 - 忽略
int main(int,char **):被捕获的异常:from int Foo :: bar



./ main 1

 
Foo: :〜Foo():被捕获的异常 - 但是*无*异常正在等待 - 重新抛出
int main(int,char **):被捕获的异常:from Foo ::〜Foo()

示例:

  //文件main.cpp 
// build with eg make main
//在Ubuntu-Karmic上使用g ++ v4.4.1测试成功
#include< iostream>

class Foo {
public:

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

〜Foo(){
bool exc_pending = std :: uncaught_exception();
try {
bar(0);
} catch(const std :: string& e){
//确保在此期间没有创建新的异常
if(std :: uncaught_exception())exc_pending = true ;

if(exc_pending){
std :: cerr<< __PRETTY_FUNCTION__
<< :caught exception - but have pending exception - ignoredoring
<< 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;
//如果没有给出参数,将在Foo :: bar()中抛出一个异常。否则
//来自Foo ::〜Foo()的异常被抛出。
f.bar(argc-1);
} catch(const std :: string& e){
std :: cerr< __PRETTY_FUNCTION__<< :caught exception:< e - < std :: endl;
}
return 0;
}

ADDED :换句话说:警告在一些文章它工作原理 - 所以可能是错误的?

解决方案

没有什么技术上错误的代码。它是完全安全的,你永远不会意外终止,因为你提出了一个例外,当它不安全。问题是它也没有用,因为它偶尔也不会在安全时抛出异常。你的析构函数的文档基本上必须说这可能或不会抛出异常。



如果偶尔不会抛出异常,你可能会从不引发异常。这样,你至少是一致的。


There are some articles concluding "never throw an exception from a destructor", and "std::uncaught_exception() is not useful", for example:

But it seems that I am not getting the point. So I wrote a small testing example (see below).

Since everything is fine with the testing example I would very appreciate some comments regarding what might be wrong with it ?

testing results:

./main

    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;
}

ADDED: In other words: despite of the warnings in some articles it works as expected - so what might be wrong with it?

解决方案

There's nothing technically wrong with your code. It's perfectly safe in that you will never accidentally terminate because you threw an exception when it was not safe to. The issue is that it also is not useful, in that it will occasionally also not throw an exception when it is safe to. Your destructor's documentation basically has to say "this might or might not throw an exception."

If it occasionally won't throw an exception, you might as well never throw an exception. That way, you're at least consistent.

这篇关于正确使用std :: uncaught_exception在析构函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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