问题描述
这些天,我一直在阅读很多,特别是。
These days, I have been reading a lot the C++ F.A.Q and especially this page.
阅读本部分我发现了一个技术,作者称为异常分派器,允许有人在一个方便的函数中将他的所有异常处理分组:
Reading through the section I discovered a "technique" that the author calls "exception dispatcher" that allows someone to group all his exception handling in one handy function:
void handleException()
{
try {
throw; // ?!
}
catch (MyException& e) {
//...code to handle MyException...
}
catch (YourException& e) {
//...code to handle YourException...
}
}
void f()
{
try {
//...something that might throw...
}
catch (...) {
handleException();
}
}
> throw; 语句:如果你考虑给定的例子,那么肯定,它是显而易见的:它重新抛出异常首先捕获在 f()
What bothers me is the single throw;
statement: if you consider the given example then sure, it is obvious what it does: it rethrows the exception first caught in f()
and deals with it again.
但是如果我自己调用 handleException()
从 catch()
子句执行它?是否有任何指定的行为?
But what if I call handleException()
on its own, directly, without doing it from a catch()
clause ? Is there any specified behavior ?
此外,对于奖励积分,是否有任何其他奇怪(可能不是好词)使用 / code>您知道吗?
Additionally for bonus points, is there any other "weird" (probably not the good word) use of throw
that you know of ?
谢谢。
推荐答案
如果你自己做一个 throw;
,并且没有当前的异常重新抛出,那么程序突然结束。 (更具体地说, terminate()
被调用。)
If you do a throw;
on its own, and there isn't a current exception for it to rethrow, then the program ends abruptly. (More specifically, terminate()
is called.)
是重新抛出当前异常的唯一安全方式 - 它不等于
Note that throw; is the only safe way to re-throw the current exception - it's not equivalent to
catch(exception const& e){throw e; }
这篇关于什么是单个“投掷”语句do?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!