异常处理
来源:互联网 发布:剑三长歌脸型数据 编辑:程序博客网 时间:2024/06/08 12:22
例一:
#include<iostream>using namespace std;//double div(double x, double y) throw(int, double) //限制抛出的类型,如不匹配,直接抛给内核//double div(double x, double y) throw() //直接抛给内核double div(double x, double y) //没有限制{ if(y==0) throw "exception"; cout<<"div(x, y)"<<endl; return x/y;}void main(){ try{ try{ divison(5, 0); } catch (int errcode){ cout << "div can't be 0!" << errcode << endl; //throw 10; throw;//继续抛一样的 } catch (double errcode){ cout << "div can't be 0.0!" << errcode << endl; //throw 10.0; throw; } catch (...){//其他类型异常 cout << "div can't be 0.0!" << endl; throw; } } catch (int errcode){ cout << "div can't be 0.0!" << errcode << endl; } catch (double errcode){ cout << "div can't be 0.0!" << errcode << endl; } catch (...){//其他类型异常 cout << "div can't be 0.0!" << endl; } cout << "main end!" << endl;}
例二:异常嵌套
#include<iostream>using namespace std;double division(double x, double y) throw(){ if (y == 0) //throw "exception"; throw 100; cout << "div(x,y)" << endl; return x / y;}void fun(){ try{ division(5, 0); } catch (int errCode) { cout << "first exception, " << errCode << endl; throw; }}int main(void){ try{ fun(); } catch (int errCode){ cout << "catched exception, " << errCode << endl; } catch (double errCode){ cout << "catched exception, " << errCode << endl; } catch (...){//其他类型异常 cout << "catched ..." << endl; } cout << "main end" << endl; return 0;}
例三:异常类
#include<iostream>using namespace std;class MyException{ public: MyException(string errInfo="unknown", int errNo=0) :errInfo(errInfo), errNo(errNo) { cout<<"MyException()"<<endl; } ~MyException() { cout<<"~MyException()"<<endl; } MyException(const MyException &) { cout << "MyException(MyException&)" << endl; } void out() { cout<<"no:"<<errNo<<", info:"<<errInfo<<endl; } private: string errInfo; int errNo;};void fun(int code){ if(code==110) { //throw MyException("terrist", 110);//首选,保证唯一性,且在传递时不会丢失 MyException temp("terrist", 110); throw temp; } if(code==120) { MyException *MyEx=new MyException("help", 120); throw MyEx; } if(code==119) { MyException MyEx("fire", 119); throw &MyEx; //不要返回局部变量的地址 }}main(){ cout<<"please input errnun:"; int code; cin>>code; try{ fun(code); }/*catch(MyException &MyEx) //类型为MyException的异常将会--> { cout<<"reference"<<endl; MyEx.out(); }*/catch(MyException MyEx) //-->为MyException更早的处理者所捕获 { cout<<"value"<<endl; MyEx.out(); }catch(MyException *MyEx) { cout<<"pointer"<<endl; MyEx->out(); //对于120而言才需要; delete MyEx; }}
阅读全文
0 0
- 异常处理+异常+处理异常+自定义异常
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 2017 计蒜之道初赛第六场
- 图片修饰导航实现
- 用库函数sort的cmp函数排数组的时候如果数组里面有longlong的数 cmp参数应该传longlong
- npm WARN deprecated minimatch@0.3.0: Please update to minimatch 3.0.2 or higher to avoid a RegExp Do
- 记一次kafka消费能力优化
- 异常处理
- java语言基础|关于子类和父类的构造方法
- Hadoop 相关概念
- 495. Teemo Attacking
- DFS and BFS (一)
- android6.0悬浮窗权限问题(eg:魅族)
- JSP
- muduo网络库
- 2017 计蒜之道 初赛 第五场——A. UCloud 机房的网络搭建