拦截 c++例外情况

发布于 2024-10-03 22:16:31 字数 271 浏览 1 评论 0原文

所以我的 C++ 程序崩溃了,我得到的错误是:

terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_S_create
Aborted

现在,我最近添加到代码中的是 SIGSEGV 处理程序,因此如果它是分段错误,它将继续打印堆栈跟踪。

如何在 C++ 中为未捕获的(或更类似于不可捕获的)异常创建退出处理程序?

So my c++ program just crashed, and the error I got was:

terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_S_create
Aborted

Now, what I've added recently to my code is a SIGSEGV handler, so if it was a segmentation fault, it would proceed to print the stack trace.

How do I go about making an exit handler for uncaught (or more like uncatchable) exceptions in c++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋意浓 2024-10-10 22:16:31

使用 set_terminate 函数设置终止处理函数:

终止处理函数是
函数自动调用时
异常处理流程必须是
由于某种原因被遗弃。这
当找不到处理程序时发生
对于抛出的异常,或者对于某些
其他特殊情况
使得无法继续
处理流程。

Use set_terminate function which sets the terminate handler function:

A terminate handler function is a
function automatically called when the
exception handling process has to be
abandoned for some reason. This
happens when a handler cannot be found
for a thrown exception, or for some
other exceptional circumstance that
makes impossible to continue the
handling process.

生生漫 2024-10-10 22:16:31

添加到 @vitaut 的答案,如果您使用的是 C++11,您可以在 std::set_terminate 指定的处理程序中检查并获取当前异常。

根据 Daniel Krügler,他提到根据下面引用的标准,在调用 std::terminate 期间有一个隐式异常处理程序处于活动状态,这意味着我们可以使用 std::current_exception 来检查是否存在是一个活跃的异常并检查它。

C++11 标准工作草案 N3242,第 15.3.7 节(强调我的):

当 catch 的形式参数(如果有)初始化完成时,处理程序被认为是活动的
条款。 [ 注意:此时堆栈将被展开。 - 尾注] 此外,还有一个隐式处理程序
当由于抛出而进入 std::terminate() 或 std::unexpected() 时,被认为是活动的。处理程序
当 catch 子句退出或 std::unexpected() 在被调用后退出时,不再被认为是活动的
由于投掷而进入。



窃取 Andrzej 的 C++ 博客,以下是一个示例完成:

[[noreturn]] void onTerminate() noexcept
{
    if( auto exc = std::current_exception() ) { 
        // we have an exception
        try{
            rethrow_exception( exc ); // throw to recognize the type
        }
        catch( MyException const& exc ) {
            // additional action
        }
        catch( MyOtherException const& exc ) {
            // additional action
        }
        catch( std::exception const& exc ) {
            // additional action
        }
        catch( ... ) {
            // additional action
        }
    }

    std::_Exit( EXIT_FAILURE );
}

Adding to the answer by @vitaut, if you are using C++11 you can check for and get the current exception within the handler specified by std::set_terminate.

According to Daniel Krügler who refers to the standard quoted below, there is an implicit exception handler active during the call to std::terminate, which means that we can use std::current_exception to both check whether there is an active exception and also examine it.

The C++11 standard working draft N3242, section 15.3.7 (emphasis mine):

A handler is considered active when initialization is complete for the formal parameter (if any) of the catch
clause. [ Note: The stack will have been unwound at that point. — end note ] Also, an implicit handler
is considered active when std::terminate() or std::unexpected() is entered due to a throw
. A handler
is no longer considered active when the catch clause exits or when std::unexpected() exits after being
entered due to a throw.


Stealing from Andrzej's C++ blog, here is an example of how this can be done:

[[noreturn]] void onTerminate() noexcept
{
    if( auto exc = std::current_exception() ) { 
        // we have an exception
        try{
            rethrow_exception( exc ); // throw to recognize the type
        }
        catch( MyException const& exc ) {
            // additional action
        }
        catch( MyOtherException const& exc ) {
            // additional action
        }
        catch( std::exception const& exc ) {
            // additional action
        }
        catch( ... ) {
            // additional action
        }
    }

    std::_Exit( EXIT_FAILURE );
}
笑饮青盏花 2024-10-10 22:16:31

只需将 try-catch(...) 放在程序的 to 级别即可。像这样的东西:

try {
   doStuff();
} catch( std::exception& e ) {
  //handle std::exception-derived exceptions
} catch(...) {
  //handle all other exceptions
}     

Just put try-catch(...) at the to level of your program. Something like this:

try {
   doStuff();
} catch( std::exception& e ) {
  //handle std::exception-derived exceptions
} catch(...) {
  //handle all other exceptions
}     
怂人 2024-10-10 22:16:31

您可以使用 std::set_terminate< 安装自己的终止处理程序/a>.

您可以使用 catch-all 子句 catch (...) {} 捕获所有 C++ 异常。

You can install your own terminate handler with std::set_terminate.

You can catch all C++ exceptions with a catch-all clause catch (...) {}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文