c++学习笔记--异常处理
来源:互联网 发布:生死手游刷金币软件 编辑:程序博客网 时间:2024/06/05 04:53
代码1.
int main(int argc, const char *argv[]){ try { cout << "foo" << endl; throw std::runtime_error("exception ..."); cout << "bar" << endl; } catch(...) { cout << "In catch .." << endl; } cout << "continue ..." << endl; return 0;}
这段代码的输出结果:
foo
In catch
continue...
程序在运行try时,执行到 throw语句 ,抛出异常去执行 catch内的内容,而throw下面的 cout<<"bar"<<endl;不会执行
2.
int main(int argc, const char *argv[]){ int a, b; try { cin >> a >> b; if(b == 0) throw runtime_error("除数不能为零!"); int res = a / b; cout << "res = " << res << endl; } catch(exception &e) { cout << "异常信息: " << e.what() << endl; } return 0;}
如果输入的b的值为0 则抛出异常 ,执行catch语句 里面的内容 “异常信息:除数不能为零!”。如果b不是0则执行 throw下面的语句。
3.
int main(int argc, const char *argv[]){ cout << "Before ..." << endl; //throw exception(); throw runtime_error("出现异常"); cout << "After .." << endl; return 0;}
这段代码编译不会通过,因为当执行 throw语句时 ,没有catch语句来处理异常。
4.
//对异常的捕获处理int main(int argc, const char *argv[]){ try { throw runtime_error("出现运行期错误"); } catch(exception &e) { cout << "异常信息:" << e.what() << endl; } cout << "继续运行" << endl; return 0;}
5.
//对于不同的异常可以采取不同的catch块进行捕捉int main(int argc, const char *argv[]){ try { int i; cin >> i; if(i == 0) throw runtime_error("出现运行期错误"); else if(i == 1) throw invalid_argument("非法参数"); } catch(runtime_error &e) { cout << "runtime_error :" << e.what() << endl; } catch(invalid_argument &e) { cout << "invalid_argument:" << e.what() << endl; } cout << "继续运行" << endl; return 0;}6.
//异常可以统一处理int main(int argc, const char *argv[]){ try { int i; cin >> i; if(i == 0) throw runtime_error("出现运行期错误"); else if(i == 1) throw invalid_argument("非法参数"); } catch(exception &e) { cout << "exception :" << e.what() << endl; } cout << "继续运行" << endl; return 0;}
7.
//异常捕获不到,照样因为core dumpint main(int argc, const char *argv[]){ try { int i; cin >> i; if(i == 0) throw runtime_error("出现运行期错误"); else if(i == 1) throw invalid_argument("非法参数"); } catch(runtime_error &e) { cout << "runtime_error :" << e.what() << endl; } cout << "继续运行" << endl; return 0;}
8.
//对于不同的异常可以采取不同的catch块进行捕捉//对于一部分可以统一处理int main(int argc, const char *argv[]){ try { int i; cin >> i; if(i == 0) throw runtime_error("出现运行期错误"); else if(i == 1) throw invalid_argument("非法参数"); else if(i == 2) throw logic_error("逻辑错误"); else throw out_of_range("越界错误"); } catch(runtime_error &e) { cout << "runtime_error :" << e.what() << endl; } catch(invalid_argument &e) { cout << "invalid_argument:" << e.what() << endl; } catch(exception &e) { cout << "异常信息:" << e.what() << endl; } cout << "继续运行" << endl; return 0;}
9.
//对于不同的异常可以采取不同的catch块进行捕捉//对于一部分可以统一处理int main(int argc, const char *argv[]){ try { int i; cin >> i; if(i == 0) throw runtime_error("出现运行期错误"); else if(i == 1) throw invalid_argument("非法参数"); else if(i == 2) throw logic_error("逻辑错误"); else throw out_of_range("越界错误"); } catch(...) //能捕获所有的异常 { } catch(exception &e) { cout << "异常信息:" << e.what() << endl; } catch(runtime_error &e) { cout << "runtime_error :" << e.what() << endl; } catch(invalid_argument &e) { cout << "invalid_argument:" << e.what() << endl; } cout << "继续运行" << endl; return 0;}这段代码会出现运行错误,因为 一开始就能捕获全部异常,那么对下面catch就失效了
10.
void test(){ int i; cin >> i; if(i == 0) throw runtime_error("出现运行期错误"); else if(i == 1) throw invalid_argument("非法参数"); else if(i == 2) throw logic_error("逻辑错误"); else throw out_of_range("越界错误");}//对于不同的异常可以采取不同的catch块进行捕捉//对于一部分可以统一处理int main(int argc, const char *argv[]){ try { test(); } catch(runtime_error &e) { cout << "runtime_error :" << e.what() << endl; } catch(invalid_argument &e) { cout << "invalid_argument:" << e.what() << endl; } catch(exception &e) { cout << "异常信息:" << e.what() << endl; } cout << "继续运行" << endl; return 0;}
catch(...) 与 catch(exception &e) 的区别在于 前者不知道异常的类型,后者可以做到
0 0
- C++&windows异常处理学习笔记
- 异常处理学习笔记
- Object-C 学习笔记(十一)---动态绑定&异常处理
- 精通C#:Chapter7 结构化异常处理-学习笔记
- C/C++学习笔记八(断言与异常处理)
- 【c++】简单异常处理笔记
- c的异常处理学习
- java 异常处理学习笔记
- java学习笔记 异常处理
- Java学习笔记(异常处理)
- iOS异常处理学习笔记
- 学习java异常处理笔记
- Python学习笔记--异常处理
- java异常处理学习笔记
- 7. 异常处理 --- 学习笔记
- c++学习笔记--异常处理
- j2se学习笔记-异常处理
- springMVC学习笔记-异常处理
- 20140917 N2
- Android/Linux驱动开发之使用dev_dbg调试设备驱动
- 不同数据库oracle mysql SQL Server DB2 infomix sybase分页查询语句
- 在Centos中yum安装和卸载软件的使用方法[转]
- 目标检测中背景建模方法
- c++学习笔记--异常处理
- 获取 web界面数据
- UVa 10870 - Recurrences (矩阵构造 矩阵快速幂)
- (入门贴)JVM堆内存相关的启动参数:年轻代、老年代和永久代的内存分配
- 使用 Spring Data JPA 简化 JPA 开发
- Remote_login_passwordfile参数探究
- SQL——基本表的创建和撤销
- JSP应用开发的背景及特点
- windows下cmake的使用