捕获子句未正确捕获

Catch clauses not catching right?

本文关键字:子句      更新时间:2023-10-16

我只是在学习如何在C++中使用异常,并且在我的"测试"代码中遇到了奇怪的行为。(请原谅像这样过于愚蠢的问题...这不是缺乏研究/努力,只是缺乏经验!如果我只捕获异常DivideByZero它工作正常。

但是引入第二个异常StupidQuestion会使代码无法完全按照我的预期工作。我在下面是如何写的,我认为如果需要,它应该处理DivideByZero异常,如果没有,请检查是否发生StupidQuestion,如果没有,只需返回try子句并打印正常结果。但是,如果我输入,比如说,a=3b=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;
}

我想知道这是否与 DivideByZeroStupidQuestion 都是 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