C++——try、throw、catch实例学习程序
来源:互联网 发布:鲁道夫·阿贝尔知乎 编辑:程序博客网 时间:2024/06/02 02:52
#include<iostream>#include<stdexcept>//exception/stdexcept/new/type_info头文件中都有定义的标准异常类using namespace std;int main(){ try{ int a,b; char s; cin>>a>>s>>b; if(s=='/'){ if(b==0) throw "Divided by 0!"; cout<<a<<"/"<<b<<"="<<a/b<<endl; } else if(s=='%') { if(b==0) throw a; cout<<a<<"%"<<b<<"="<<a%b<<endl; } else cout<<"Option must be % or /."<<endl; }//try //捕获int类型的异常并处理 catch(int i) { cout<<"Error occur:"<<i<<"%0"<<endl; } //捕获char* 类型的异常并处理 catch(char *str) {cout<<"Error occur:"<<str<<endl; } catch(runtime_error err){ cout<<err.what()<<endl;} //捕获其它任何异常并处理 catch(...){cout<<"Unkown Error"<<endl;} //没错误不运行catch,有错误至多运行一个catch语句块,且不再返回try语句块中。 //不管有无异常都要运行到此处 cout<<"Hello world"<<endl; return 0;}
0 0
- C++——try、throw、catch实例学习程序
- JavaScript——Throw、Try 和 Catch
- 【c#】异常处理:try catch throw
- 【JS学习笔记】03 try catch throw
- Android异常处理——try、catch、finally、throw、throws
- android异常处理——try、catch、finally、throw、throws
- Android异常处理——try、catch、finally、throw、throws
- Android异常处理——try、catch、finally、throw、throws
- JavaScript——错误(throw、try、catch)
- JavaScript入门之错误—Throw.Try.Catch
- JAVA中异常抛出、捕获机制—try catch throw
- try...catch...throw
- try catch throw用法
- catch,try,throw
- try catch throw用法
- try catch throw用法
- try,catch,throw
- try catch throw 使用
- C++——windows结构异常转化成C++异常
- c编程技巧——获取可用的处理器(CPU)核数
- 使用U盘安装WIN7系统疑难问题总结
- Contains Duplicate II
- LaTeX练习
- C++——try、throw、catch实例学习程序
- LeetCode——Merge Sorted Array
- UVa - 1153 - Keep the Customer Satisfied
- 反射
- jsp页面 ajax实现多图上传
- Upload files by sending multipart request programmatically
- 网络编程
- 今天工作总结和学习总结
- android 事件处理机制之requestDisallowInterceptTouchEvent