35

如果在我的代码中,我有以下代码段:

try {
  doSomething();
} catch (...) {
  doSomethingElse();
  throw;
}

throw 会重新抛出默认省略号处理程序捕获的特定异常吗?

4

1 回答 1

43

是的。异常在被捕获之前一直处于活动状态,此时它变为非活动状态。但它一直存在到处理程序的范围结束。从标准来看,强调我的:

§15.1/4:被抛出异常的临时副本的内存以未指定的方式分配,除非在 3.7.4.1 中注明。只要有针对该异常执行的处理程序,临时性就会持续存在。

那是:

catch(...)
{ // <--

    /* ... */

} // <--

在这些箭头之间,您可以重新抛出异常。只有当处理程序范围结束时,异常才会停止存在。

事实上,在 §15.1/6 中给出的示例与您的代码几乎相同:

try {
    // ...
}
catch (...) { // catch all exceptions
    // respond (partially) to exception <-- ! :D
    throw; //pass the exception to some
           // other handler
}

请记住,如果您throw没有主动异常,terminate将被调用。对于您来说,情况并非如此,在处理程序中。


如果doSomethingElse()throws 并且异常没有相应的处理程序,因为原始异常被认为已处理,新异常将替换它。(好像它刚刚抛出,开始堆栈展开等)

那是:

void doSomethingElse(void)
{
    try
    {
        throw "this is fine";
    }
    catch(...)
    {
        // the previous exception dies, back to
        // using the original exception
    }

    try
    {
        // rethrow the exception that was
        // active when doSomethingElse was called
        throw; 
    }
    catch (...)
    {
        throw; // and let it go again
    }

    throw "this replaces the old exception";
    // this new one takes over, begins stack unwinding
    // leaves the catch's scope, old exception is done living,
    // and now back to normal exception stuff
}

try
{
    throw "original exception";
}
catch (...)
{
  doSomethingElse();
  throw; // this won't actually be reached,
         // the new exception has begun propagating
}

当然,如果什么都没有抛出,throw;将会到达并且你会按预期抛出你捕获的异常。

于 2010-03-19T01:24:35.890 回答