捕获子句未正确捕获
Catch clauses not catching right?
我只是在学习如何在C++中使用异常,并且在我的"测试"代码中遇到了奇怪的行为。(请原谅像这样过于愚蠢的问题...这不是缺乏研究/努力,只是缺乏经验!如果我只捕获异常DivideByZero
它工作正常。
但是引入第二个异常StupidQuestion
会使代码无法完全按照我的预期工作。我在下面是如何写的,我认为如果需要,它应该处理DivideByZero
异常,如果没有,请检查是否发生StupidQuestion
,如果没有,只需返回try
子句并打印正常结果。但是,如果我输入,比如说,a=3
和b=1
,程序会重定向到DivideByZero
try
子句而不是StupidQuestion
子句。然而,奇怪的是,divide
似乎确实在抛出StupidQuestion
(见cout
声明),但它并没有正确,正如cout
声明的无意义所看出的那样。
#include <iostream>
#include <cstdlib>
using namespace std;
const int DivideByZero = 42;
const int StupidQuestion=1337;
float divide (int,int);
main(){
int a,b;
float c;
cout << "Enter numerator: ";
cin >> a;
cout << "Enter denominator: ";
cin >> b;
try{
c = divide(a,b);
cout << "The answer is " << c << endl;
}
catch(int DivideByZero){
cout << "ERROR: Divide by zero!" << endl;
}
catch(int StupidQuestion){
cout << "But doesn't come over here...?" << endl;
cout << "ERROR: You are an idiot for asking a stupid question like that!" << endl;
}
system("PAUSE");
}
float divide(int a, int b){
if(b==0){
throw DivideByZero;
}
else if(b==1){
cout << "It goes correctly here...?" << endl;
throw StupidQuestion;
}
else return (float)a/b;
}
我想知道这是否与 DivideByZero
和 StupidQuestion
都是 int
型这一事实有关,所以我更改了代码以使 StupidQuestion 成为 char 类型而不是 int。 (所以:const char StupidQuestion='F';
和catch(char StupidQuestion)
实际上是上面唯一改变的东西)它工作得很好。
当两个异常具有相同的类型(int
)时,为什么上面的代码不起作用?
而不是这个
catch(int DivideByZero) {
cout << "ERROR: Divide by zero!" << endl;
}
catch(int StupidQuestion) {
cout << "But doesn't come over here...?" << endl;
cout << "ERROR: You are an idiot for asking a stupid question like that!" << endl;
}
您正在寻找
catch (int errval) {
if (errval == DivideByZero) {
cout << "ERROR: Divide by zero!" << endl;
}
else if (errval == StupidQuestion) {
cout << "ERROR: You are an idiot for asking a stupid question like that!" << endl;
}
else {
throw; // for other errors, keep searching for a handler
}
}
catch
子句中的变量名正在创建一个新的局部变量,该变量与具有相同名称的全局常量无关。
另请注意,无法只捕获一个错误号...但是您可以重新抛出未知错误,如我所示。
catch(int DivideByZero) { }
catch(int StupidQuestion) { }
两个catch
块都捕获int
,它们只是名称不同。只能输入第一个,第二个是死代码。
当为异常选择处理程序时,只考虑类型,并且既不考虑值也不考虑地址(由于异常的工作方式,变量的地址在这里根本不适用),编译后也不存在变量的名称。始终为异常选择第一个适当的处理程序。
详情请查看我对另一个问题的回答:https://stackoverflow.com/a/45436594/1790694
- 如何理解C++标准N3337中的expr.const.cast子句8
- if 子句中的赋值不起作用
- C++20 概念:如何在"requires"子句中引用类名?
- 尝试将另一个子句添加到代码中时出错
- 对OpenMP reduction子句中的变量执行原子操作
- 要求子句中不允许哪些替换失败?
- "using namespace"子句在什么范围内有效?
- 在编译时计算"if"子句
- OpenMP 条件并行化 - 并行部分中 if 子句的语法
- 在 CPP 中使用正则表达式将密码("ABC")子句屏蔽为密码("****")
- 如何加快有序子句的 openmp 并行速度?
- 当使用 require 表达式指定和不使用 require 表达式指定 require 子句时,C++ gcc HEAD
- if子句中的有条件分配
- 谷歌模拟 - 保存EXPECT_CALL然后重复使用和更改子句
- 如何摆脱If-Elif-Else子句
- 为什么可变参数模板在模板介绍中不起作用,但在要求子句中起作用?ConceptName{T,U,V,W} <-- template<typename ...T>
- 在 SQLAPI c++ 中向 IN 子句添加多个参数
- 在C 中使用OpenMP共享子句
- 在if子句中自动分配
- 错误:"num_thread"之前应包含"#pragma OMP"子句