gpt4 book ai didi

c++ - 抛出多个异常时,catch block 的执行顺序是什么?为什么?

转载 作者:太空宇宙 更新时间:2023-11-04 15:11:37 25 4
gpt4 key购买 nike

<分区>

在下面的代码中,函数在一个语句中抛出两个异常。现在,为什么是 int catch block 处理异常而不是另一个 block ?最后一个异常被处理的情况总是这样吗?


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);
}


25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com