异常处理

来源:互联网 发布:lte mrr弱覆盖优化 编辑:程序博客网 时间:2024/04/30 13:38
**#include <iostream>#include <fstream>#include <cstring>#include <strstream>using namespace std;int main(){    void f1();    try{f1();}    catch(double){cout <<"OK0!"<<endl;}    cout<<"end0"<<endl;    return 0;}void f1(){    void f2();    try {f2();}    catch(char)    {cout<<"OK1!"<<endl;}    cout<<"endl"<<endl;}void f2(){void f3();    try{f3();}    catch(int)    {cout<<"OK2!"<<endl;}    cout<<"end2"<<endl;}void f3(){double a=0;try{throw a;}    catch(float)    {cout<<"OK3!"<<endl;}    cout<<"end3"<<endl;}**

try-catch 语句可以与throw出现在同一个函数中,也可以不在同一个函数中。当throw跑出异常信息后,首先在本函数中寻找与之匹配的catch,如果在本函数中无try-catch结构或则找不到与之匹配的catch,就转到其上一层去处理,如果上一层也无try-catch结构或找不到与之匹配的catch,则在转到更上一层的try-catch结构去处理,也就是说转到离开出现异常最近的try-catch结构去处理。

0 0
原创粉丝点击