浮点异常不可捕获
floating point exception not catchable?
本文关键字:异常 更新时间:2023-10-16
浮点异常有问题。当我除以零时,我得到这个异常。我试图抓住它,但互联网上的解决方案对我不起作用。
#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
我建议保护输入验证。
在你的代码中,异常发生在你开始抛出东西之前。
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 尝试使用智能指针时引发异常
- 函数如何通知用户它基于函数原型抛出异常?
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- Pytorch torch.cholesky忽略异常