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
#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
相关文章推荐
- C++学习笔记:高级编程:文件和流,异常处理,动态内存,命名空间
- 黑马程序员 - 学习笔记9 异常处理的初步了解
- C++ FAQ学习笔记 17章 异常和错误处理
- 未处理异常和C++异常——Windows核心编程学习手札之二十五
- C++简单学习(Part3_lecture 10)(文件、流、异常处理、动态内存、命名空间)
- C++学习总结系列--异常处理
- 学习c++ ,对异常错误输入的处理
- C/C++学习----第六章 异常处理
- c++学习之异常处理简单用法
- C++学习笔记——异常处理匹配与函数嵌套
- java异常处理-初步学习
- C++学习笔记(6)-异常处理
- C++异常处理的学习
- c++ 异常处理 的简单学习
- C/C++学习----第六章 异常处理
- Symbian学习总结-基础篇-Symbian C++中的异常处理
- C++ 异常处理
- c#通过Graphics处理图片以及画图(初步学习1)
- C与C++中的异常处理
- C++ 异常处理