Catch(...)无法捕获浮点异常 [英] Catch (...) fails to catch floating point exception

查看:152
本文介绍了Catch(...)无法捕获浮点异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道catch(...)应该捕获任何异常。但是,在下面的程序中,catch(...)无法捕获浮点(除以零)异常。

I understood that catch (...) was supposed to catch any exception. In the following program, however, catch (...) fails to catch a floating point (divide by zero) exception.

#include <iostream>
using namespace std;

int main () 
  {
    int top = 90;
    int bottom = 0;
    
    try
      {
        cout << "top / 2 = " << top / 2 << endl;
        cout << "top / bottom = " << top / bottom << endl;
      }
    catch ( ... ) 
      {
        cout << "We have a problem." << endl;
      }
    cout << "Done" << endl;
    return (0);
  }



C ++规范是否已更改,或者程序有问题?



非常感谢,



TCNM


Has the C++ specification changed, or is something wrong with the program?

Thank you very much,

TCNM

推荐答案

这里回答:

http://stackoverflow.com/questions/6121623/catching-exception-divide-by-zero [ ^ ]


如果你正在使用Visual Studio,那么有一个构建选项来打开/关闭浮点异常配置属性\ C / C ++ \Code生成,它至少会让它抛出(添加/ fp:除了命令行)。正是你必须抓住我不确定但是 catch(...)应该有效。正如Victor所说,虽然它不是标准的,但不便携。
If you''re using Visual Studio there is s build option to turn on/off floating point exceptions Configuration Properties\C/C++\Code Generation which will at least get it to throw ( adds /fp:except to comand line ). Exactly what you''d have to catch I''m not sure but catch(...) should work. As Victor indicates though it''s not standard so not portable.


这篇关于Catch(...)无法捕获浮点异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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