您的位置:首页 > 编程语言 > C语言/C++

C++的异常处理初步学习

2014-05-18 16:44 260 查看
异常处理的预习

#include<iostream>
using namespace std;

void f3(int x) {
switch(x) {
case 1:throw 3.4;
case 2:throw 2.5f;
case 3:throw 1;
}
cout << "end of F3" << endl;
}

void f2(int x) {
try {
f3(x);
}
catch(int) {cout << "int error from f2" << endl;}
catch(float) {cout << "float error from f2" << endl;}
cout << "end of f2" << endl;
}

void f1(int x) {
try {f2(x);}
catch (int) {
cout << "int error from f1\n";
}
catch (float) {
cout << "float error from f1\n";
}
catch (double) {
cout << "double error from f1\n";
}
cout << "end of f1" << endl;
}

int main() {
for (int i = 1; i < 4; i++)
f1(i);
cout << "end of main" << endl;
return 0;
}

函数调用顺序:main  ====>  f1  =====>  f2  =====>  f3

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: