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

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

问题描述

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



我在Java中了解到,只需要捕获一个异常。但C ++如何处理?



[更新]我明白C ++中也有异常处理,但Segfault不是例外,是吗?当segfault发生时,我不认为任何东西被抛出。你必须明确地扔一些东西来使用try .... catch ....据我所知。非常感谢,我对C ++来说非常新鲜。

解决方案

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


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?

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

[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.

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.

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

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