浮点异常无法捕获? [英] floating point exception not catchable?

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

问题描述

我对浮点异常有疑问。当我被零除时,我得到这个异常。我试图抓住它,但是互联网上的解决方案对我不起作用。

I have a problem with the floating point exception. When i divide by zero, i get this exception. I tried to catch it, but the solutions on the internet don't work for me.

#include<iostream>

using namespace std;

int main(){

double h{0};
int a{0},b{0},c{0};

cin.exceptions(ios_base::failbit);

cout << "Enter Values: ";
try{ 
  cin >> a >> b >> c;  
  h = (3/1/a+1/b+1/c);      

if(a == 0 || b == 0 || c == 0){
 throw overflow_error("Division by zero is not allowed!");
}
  cout << h;
}
catch(overflow_error e){
     cerr << e.what();
 }
 catch(exception& e){
     cerr << "Only numbers!";
 }
 catch(...){
    cerr << "?";
}
return 0;
}


推荐答案

您将获得一个浮点异常(这不是C ++异常),这就是为什么它不可捕获的原因。

You’re getting a "floating point exception" (which is not a C++ exception), that's why its not catchable.

https://www.quora.com/Why-isn%E2%80% 99t-this-catch-block-in-C ++-捕获任何异常

我建议保护进行输入验证。

在您的代码中,异常发生在您开始扔东西之前。

In your code, the exception happens before you start throwing stuff.

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

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