c++ - 一个异常可以触发多个 catch 块吗?
问题描述
我想问这个问题的最简单方法是看看“Caught Potato except”是否会被打印出来。
我知道“捕获的蔬菜除外”会被打印出来,但是在那个捕获块之后它会按顺序继续并尝试查看其他捕获块是否也可以工作?
class Vegetable_Except {};
class Potato_Except : public Vegetable_Except{};
int func(int n){
if(n < 0)
throw Potato_Except();
cout << "n: " << n << " ";
return func(--n);
}
int main(int argc, char ** argv) {
try{
func(3);
func(2);
}
catch(Vegetable_Except & e){
cout << "Caught Vegetable Except" << endl;
}
catch(Potato_Except & e){
cout << "Caught Potato Except" << endl;
}
cout << "We survived the vegetables" << endl;
return 0;
}
代码略微改编自 UMich F2020 EECS280。
解决方案
推荐阅读
- python - 如何为不属于任何应用程序的 URL 创建视图函数?
- scala - 如何调试和修复发散隐式
- c - 如何完成“字母”功能?(链表)
- dart - 从飞镖中的未来返回一个值
- reactjs - JSX:你可能需要一个合适的加载器来处理这个文件类型
- python - 如何在 Protobuf 字节字段中保存数据“无”?
- node.js - AWS-DocumentDB 不支持 $lookup 运算符,AWS-DocumentDB 中 $lookup 的任何替代项
- html - 如何在触摸设备上禁用点击活动样式(可点击 DIV)?
- python - 如何使用 Tkinter 和 Tensorflow 避免 SIGSEGV 错误?
- laravel - 多个 orWheres 不会在 Laravel 中工作