放开/追上堆栈安全吗?

utn*_*tim 1 c++ exception-handling stack-unwinding

问:在堆栈展开时引发并捕获异常是安全的,还是应用程序terminate在第二次引发时调用?

最小的例子:

void some_function()
{
    try
    {
        // do stuff here that can throw
        throw std::runtime_error("blah");
    } catch(const std::exception& re)
    {
        try // this code could be in some function called from here
        {
            // do something with re here that throws a logical_error
            throw std::logical_error("blah blah"); // does this call terminate?
        } catch(const std::logical_error& le)
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

看完这个问题我很好奇。

注意:我知道您可以/应该catch(...)在析构函数中使用,但是通常try/catch在一个catch块中包含有意义-也许在调用异常的某些函数中(re在我的示例中)?

Pub*_*bby 5

在堆栈展开时并不是真的。输入捕获块后,堆栈便已解卷。

是的,该代码是合法的。看到这个问题:在C ++异常处理程序中嵌套try ... catch?