c++ - 当飞行中出现多个异常时会发生什么,如何捕捉它们?

标签 c++ exception

我希望我能说得更明确一些,但是有很多可能的情况,其中有几个正在运行的异常——所有都涉及析构函数,但仍然如此。

C++98 到 C++17 的行为有何不同?

我敢预言“飞行中有几个异常就没有办法捕捉”。

最佳答案

您可能会阅读 throw 的文档.

If any function that is called directly by the stack unwinding mechanism, after initialization of the exception object and before the start of the exception handler, exits with an exception, std::terminate is called. Such functions include destructors of objects with automatic storage duration whose scopes are exited, and the copy constructor of the exception object that is called (if not elided) to initialize catch-by-value arguments.

因此 std::terminate 被调用而不是 “飞行中的几个异常”

你可能还有几个异常,只要它们不退出各自的函数:

void foo()
{
    struct S
    {
        ~S()
       {
           try {
               throw std::runtime_error("in S");
           } catch (const std::exception& ex) {
               std::err << ex.what(); // "in S"
           }
       }
    } s;

    throw std::runtime_error("In Foo");
}

关于c++ - 当飞行中出现多个异常时会发生什么,如何捕捉它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59137374/

相关文章:

java - Compact Language Detector 2 的检测方法线程安全吗?

c++ - 为什么我不能访问具有 protected 重载的公共(public)函数?

python - 如何向 while 循环添加 ValueError 异常?

android - Illegal State Exception错误-Android

c# - 静态抛出类 : good or bad practice

C++ move 语义和异常

c++ - strand 在 boost asio 中的优势是什么?

c++ - 为什么编译器不自动内联自由定义的函数?而是导致链接器错误

c++ - 在什么语言中有 "guard"关键字或概念?

java - 匹配字符串 ÷x% 的正则表达式