catch(...)在throw上工作;没有对象? [英] Does catch (...) work on throw; with no object?
问题描述
当没有待处理的异常处理在堆栈中更高的时候,C ++标准会对下面的代码发生什么?
What does C++ standard say should happen for the following code when there is no pending exception being processed higher up the stack?
try {
throw;
} catch (...) {
cerr << "Caught exception." << endl;
}
没有对象的throw会被捕获吗?
Will the throw with no object be caught or not?
推荐答案
从2003 C ++标准§15.1[except.throw] / 8:
From the 2003 C++ Standard §15.1[except.throw]/8:
如果当前没有异常处理,则执行不带操作数调用
terminate()
的 throw-expression p>
If no exception is presently being handled, executing a throw-expression with no operand calls
terminate()
.
所以,在你的例子中,因为当前没有异常处理,所以不会抛出任何异常, terminate / code>。由于
terminate()
不返回,您的 catch
块将永远不会输入。
So, in your example, since no exception is currently being handled, nothing is thrown and instead terminate()
is called. Since terminate()
does not return, your catch
block will never be entered.
这篇关于catch(...)在throw上工作;没有对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!