【问题标题】:Exception gets thrown but isn't caught in c++ program异常被抛出但未在 C++ 程序中捕获
【发布时间】:2018-03-06 19:23:54
【问题描述】:

我正在为大学练习编写一个 c++ 程序,他们希望我们抛出一些异常,然后捕获它们并打印一条消息。如您所见,如果给定分母中的任何一个为 0,我将抛出 Denominator_Is_Zero 异常,但如果您自己运行程序,您会看到它在抛出异常时崩溃,这意味着它不会得到完全抓住了。我在这里错过了什么?

#include "std_lib_facilities.h"

// function forward declarations
void calculateResults(int, int, int, int);
void checkIfDenomIsZero(int , int);

int main() {
  class Denominator_Is_Zero{};
  class Negative_Sqrt_Argument{};

  cout << "Plese insert four integers: \n"; // prompt the user for input
  int numin1,denom1,numin2,denom2; // fraction related variables declaration
  cin >> numin1 >> denom1 >> numin2 >> denom2;
  try {
    checkIfDenomIsZero(denom1,denom2);
    // Otherwise start calculating the desired fraction related values
    calculateResults(numin1,denom1,numin2,denom2);
  } catch(Denominator_Is_Zero &diz) {
    cout << "caught";
    std::cerr << "The denominator cannot be 0!" << '\n';
  } catch(Negative_Sqrt_Argument &nsa) {
    std::cerr << "The square root's argument cannot be negative!" << '\n';
  }
  return 0;
}

// This function checks if either one of the fractions's denominators is 0 and if that's the case, throws an exception
void checkIfDenomIsZero(int denom1,int denom2) {
  class Denominator_Is_Zero{}; // An exception that is thrown when either one of the fractions's denominators is 0
  if(denom1 == 0 || denom2 == 0) {
    throw Denominator_Is_Zero();
  }
}

/**
* This function takes in each fraction's numinator and denominator and
* using the fractions's LCM(Least Common Multiplier) implicitly (ar1 *= par2 and ar2 *= par1), it turns them into homonyms and then
* and then subtracts them. After that, if the subtracted numinator and denominator values
* pass the if() check, it calculates their Square Root and print the desired result to the console.
*/
void calculateResults(int num1,int den1,int num2,int den2) {
  // An exception that is thrown when either the numinator or the denominator of the subtracted fraction is negative (A square root cannot take negative values as arguments)
  class Negative_Sqrt_Argument {};
  num1 *= den2;
  num2 *= den1;
  double resNuminator = num1 - num2;
  double resDenominator = den1*den2;

  // Throw the exception based on the condition described in the exception's comment
  if(resNuminator < 0 || resDenominator < 0) {
    throw Negative_Sqrt_Argument();
  } else { // If the condition is false, then calculate the square root values of the resNuminator and resDenominator
    double sqrtResNum = sqrt(resNuminator);
    double sqrtResDen = sqrt(resDenominator);
    cout << sqrtResNum << "/" << sqrtResDen; // Print the desired result to the console
  }
}

【问题讨论】:

  • checkIfDenomIsZero 中的类型Denominator_Is_Zeromain 中声明的类型Denominator_Is_Zero 不同。在命名空间级别声明您的异常,而不是在本地声明。
  • 我想过,但如果我不在那里添加它们,那么我会得到一个:“....”不是一种类型
  • 你也应该赶上const&amp;
  • @SteliosPapamichael 您是否尝试在全局范围内定义它们,而不是在函数范围内?
  • @AlgirdasPreidžius 谢谢,就是这样。我完全忘记了。

标签: c++ exception cmd windows-10 try-catch


【解决方案1】:

您声明了两个不同的Denominator_Is_Zero。一个在main,一个在checkIfDenomIsZero

这两个Denominator_Is_Zero 类不可互换,因为它们都在两个不同的范围内声明。

您需要声明一个Denominator_Is_Zero 类,从maincheckIfDenomIsZero 都可以看到它。在您的程序中,全局定义一个 Denominator_Is_Zero 类就足够了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 2013-05-07
    • 2011-04-08
    • 2011-08-08
    • 2021-05-08
    相关资源
    最近更新 更多