如何在Linux C ++中捕获系统级异常? [英] How do I catch system-level exceptions in Linux C++?

查看:749
本文介绍了如何在Linux C ++中捕获系统级异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未调用以下catch():

The following catch() is not called:

void test(void)
{
    int i=1,j=0,k;
    try
    {
        k = i/j;
    }
    catch(...)
    {
        ...handle it...
    }
}

有没有办法捕捉这种异常?

Is there a way to catch this kind of exception?

推荐答案

请检查
http://linux.die.net/ man / 1 / gcc
有一个编译器选项-mcheck-zero-division来处理这个。

Please check http://linux.die.net/man/1/gcc there is a compiler option -mcheck-zero-division to handle this.

或者,安装SIGFPE处理程序作为一个选项,
一个float div由0生成一个'FPE_ZERODIVIDE'

Alternatively, installing a SIGFPE handler might be an option, A float div by 0 would then generate a 'FPE_ZERODIVIDE'

         signal(SIGFPE, (fptr) FPE_ExceptionHandler);

         void FPE_ExceptionHandler(int nSig,int nErrType,int */*pnReglist*/)
          {
                switch(nErrType)
                  {
                    case FPE_ZERODIVIDE:  /* ??? */ break;
                }

            }

大多数浮点系统基于IEEE标准,允许除以0.根据数字的符号,它可以适当返回正无穷大或负无穷大。 (除了0/0之外,返回未定义的NAN - 再也不是例外情况)。这往往对科学和数学应用是有用的。 NAN有效地表示计算不可靠但允许计算继续的情况。持续的计算不会产生新的结果,但将继续返回NAN。这样可以在计算结果中进行错误检查,从而实现长长的计算链。错误检查只需要在工作结束时执行。这使得代码更加简单和更快。对某些应用程序来说,它也可能更有用,无私是一个有用的结果,而不是真正的问题。

Most floating point systems are based on the IEEE standard, which allows division by 0. This returns either positive infinity or negative infinity as appropriate based on the signs of the numbers. (Except 0/0 returns the undefined NAN--again not an exceptional case.) This tends to be useful for scientific and mathematical applications. The NANs effectively signal a case where calculations were not pssible but allow calculations to continue. The continued calculations will not produce new results but will continue to return NANs. This allows long long chains of calculations to be performed witout error checking within the calculatiosn. The error checks only need to be performed at the very end of the work. This makes the code much much simpler and also faster. It can also be more useful at times as for some applications, infintity is a "useful" result, not really a sign of problems.

这篇关于如何在Linux C ++中捕获系统级异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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