抛出异常,但未在 c++ 程序中捕获
Exception gets thrown but isn't caught in c++ program
我正在为大学的练习编写一个 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
}
}
您正在声明两个不同的Denominator_Is_Zero
。一个在main
,一个在checkIfDenomIsZero
.
这两个Denominator_Is_Zero
类不可互换,因为它们分别在两个不同的作用域中声明。
您需要声明一个 Denominator_Is_Zero
类,在该类中,它从 main
和 checkIfDenomIsZero
中都可见。在您的程序中,全局定义单个Denominator_Is_Zero
类就足够了。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 如何在c++程序中找到函数的地址