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

查看:706
本文介绍了如何捕获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 by 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 - 再不是一个例外情况。)这往往对科学和数学应用程序有用。 NANs有效地表示计算不可能的情况,但允许计算继续。持续的计算不会产生新的结果,但将继续返回NAN。这允许在计算中错误检查时执行长的长链计算。错误检查只需要在工作的最后执行。这使得代码变得更加简单和快速。它也可以更有用的时候,对于一些应用程序,infintity是一个有用的结果,不是真的是问题的迹象。

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天全站免登陆