有些文章的结论是“永远不要从析构函数中抛出异常”,“std::uncaught_exception() 没有用”,例如:
- http://www.gotw.ca/gotw/047.htm (作者:赫伯·萨特)
不过我好像没听懂。所以我写了一个小测试示例(见下文)。
由于测试示例一切正常,我非常感谢您提出有关它可能有什么问题的评论?
测试结果:
./主要
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/