【发布时间】:2019-08-09 06:21:04
【问题描述】:
在下面的代码中,函数在一个语句中抛出两个异常。现在,为什么 int catch 块处理异常而不是另一个块? 是否总是最后一个异常被处理?
try
{
quotient = safe_divide(numerator , denominator);
}
catch(DivideByZero)
{
cout << "Error: Division by zero!\n"
<< "Program aborting.\n";
system("pause");
}
catch (int )
{
cout << "got you " << endl;
cout << "top : " << numerator << endl;
system("Pause");
exit(0);
}
double safe_divide(int top, int bottom) throw(DivideByZero,int)
{
if(bottom == 0)
throw (DivideByZero(),top);
return top/static_cast<double>(bottom);
}
【问题讨论】:
-
在下面的代码中,函数在一个语句中抛出了两个异常。 - 它没有,只有
top被抛出,而DivideByZero()的结果被丢弃,因为到逗号运算符 -
你的意思是像
throw DivideByZero(top);这样的东西吗?
标签: c++ exception try-catch throw