在C ++中捕获Segfault或任何其他错误/异常/信号,就像在Java中捕获异常一样 [英] Catch Segfault or any other errors/exceptions/signals in C++ like catching exceptions in Java

查看:170
本文介绍了在C ++中捕获Segfault或任何其他错误/异常/信号,就像在Java中捕获异常一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个基于一个buggy开源库的Linux程序。这个库有时会触发我无法控制的段错误。当然,一旦库有segfaults,整个程序死亡。但是,我必须确保我的程序保持运行,即使库有segfaults。这是因为我的程序类作为一个服务器,它需要至少告诉客户端发生了什么坏事,并从错误中恢复,而不是鸡出来...有没有办法做到吗?

I wrote a Linux program based on a buggy open source library. This library sometimes triggers segfaults that I cannot control. And of course once the library has segfaults, the entire program dies. However, I have to make sure my program keeps running even if the library has segfaults. This is because my program sort of serves as a "server" and it needs to at least tell the clients something bad happened and recover from the errors, rather than chicken out... Is there any way to do that?

我理解在Java中只需要捕获异常。但是C ++如何处理呢?

I understand in Java one just needs to catch an exception. But how does C++ handle this?

[UPDATE]我知道C ++中也有异常处理,但Segfault不是一个例外,是吗?我不认为当segfault发生时抛出任何东西。你必须显式地抛出东西使用try .... catch ....据我所知。

[UPDATE]I understand there is also exception handling in C++, but Segfault is not an exception, is it? I don't think anything is thrown when segfault happens. You have to explicitly "throw" something to use try.... catch.... as far as I know.

非常感谢,我对C ++很新。

Thanks so much, I am quite new to C++.

推荐答案

您不能可靠地在分段违例后恢复执行。如果您的程序必须保持运行,请在单独的进程中隔离违例库,并通过管道与其通信。当发生细分违规时,您的程序会注意到已关闭的管道。

You cannot reliably resume execution after a segmentation violation. If your program must remain running, fence off the offending library in a separate process and communicate with it over a pipe. When it takes a segmentation violation, your program will notice the closed pipe.

这篇关于在C ++中捕获Segfault或任何其他错误/异常/信号,就像在Java中捕获异常一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆