#include<iostream>#include<exception>using namespace std;class A{};int func1 (int m,int n){ try{ if(n==0) throw A(); cout <<"in funcl" <<endl; return m/n; } catch(exception){ cout <<"catched in funcl"<<endl; } cout << "before end of funcl"<<endl; return m/n;}int main() { try{ func1(5,0); cout << "in main" <<endl; } catch(A&){ cout <<"catched in main"<<endl; } cout <<"end of main" <<endl; return 0;}写出输出结果
时间: 2024-03-21 12:42:13 浏览: 45
#include <iostream>
当运行这段代码时,输出结果如下:
```
catched in funcl
before end of funcl
catched in main
end of main
```
解释一下这个输出结果:
1. 首先,我们调用`func1(5,0)`,由于`n`等于0,所以会抛出一个`A`类型的异常。
2. 异常被捕获后,`func1`函数输出字符串"catched in funcl",然后执行语句`cout << "before end of funcl" << endl;`,输出字符串"before end of funcl",最后返回`m/n`的结果。
3. 控制权回到`main`函数中,捕获到了`A`类型的异常,输出字符串"catched in main"。
4. 执行语句`cout << "end of main" << endl;`,输出字符串"end of main"。
5. 程序正常结束,返回0。
阅读全文