簡體   English   中英

浮點異常無法捕獲?

[英]floating point exception not catchable?

我對浮點異常有疑問。 當我被零除時,我得到這個異常。 我試圖抓住它,但是互聯網上的解決方案對我不起作用。

#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 ++異常),這就是為什么它不可捕獲的原因。

https://www.quora.com/Why-isn%E2%80%99t-this-catch-block-in-C++-catching-any-exception

我建議守着輸入驗證。

在您的代碼中,異常發生在您開始扔東西之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM