是否可以通过try/catch捕获段错误? [英] Is it possible to catch a segfault with try/catch?

查看:84
本文介绍了是否可以通过try/catch捕获段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了此测试以查看发生了什么:

I did this test to see what happened:

try
{
    int *x = 0;
    *x = 1234;
}
catch(...)
{
    cout << "OK";
}

但是它抛出了段错误,为什么它没有捕捉到段错误?

But it throws a segfault, why does it not catch the segfault?

推荐答案

不行.

SEGFAULT 不是常规异常.

您显示的代码只是未定义的行为,可能会发生任何事情.无法保证最终会引发异常.

The code you show is simply undefined behavior, and anything may be happen. There's no guarantee it ends up throwing an exception.

这篇关于是否可以通过try/catch捕获段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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