异常处理结构
#include<bits/stdc++.h>using namespace std;/** * 异常处理 * 注意:类型必须完全匹配 * */int main() { cout<<"1--before try block"<<endl; try { cout<<"2--inside try block"<<endl; throw 10; cout<<"3--after throw"<<endl; } catch(int i) { cout<<"4--in catch block 1 errcode is"<<i<<endl; } catch(char *s) { cout<<"5-in catch block 2 errcode is"<<s<<endl; } cout<<"6--after catch"<<endl; return 0;}
在函数中处理异常
#include<bits/stdc++.h>using namespace std;/** * 在函数中处理异常 * */void temperature(int t) { if(t == 100) { throw "hot"; } else if(t == 0) { throw "cold"; } else { cout<<"temperature is: "<<t<<endl; }}int main() { try { temperature(0); temperature(10); temperature(100); } catch(const char *s) { cout<<s<<endl; } return 0;}
捕获所有异常
#include<bits/stdc++.h>using namespace std;/** * 捕获所有异常 * */void errhandler(int n) throw() { try { if(n == 1) { throw n; } if(n == 2) { throw "dx"; } if(n == 3) { throw 1.1; } } catch(...) { cout<<"catch an exception"<<endl; }}int main() { errhandler(1); errhandler(2); errhandler(3); return 0;}
嵌套异常捕获
#include<bits/stdc++.h>using namespace std;/** * 嵌套异常捕获 * */void fc( ){ try{ throw "help"; } catch (int x) { cout<< "in fc int handler"<<endl; } try { cout<<"no error handler"<<endl; } catch (const char *px) { cout<<"in fc char * handler"<<endl; } }void fb() { int *q = new int(10); try { fc(); cout<<"return form fc(0)"<<endl; } catch(...) { delete []q; throw; }}void fa() { char *p = new char[10]; try { fb(); cout<<"return form fb(0)"<<endl; } catch(...) { delete []p; throw; }}int main() { try { fa(); cout<<"return form fa"<<endl; } catch(...) { cout<<"in main"<<endl; } cout<<"end"<<endl; return 0;}
构造函数异常
#include<bits/stdc++.h>using namespace std;/** * 构造函数异常 * */class A { int a;public: A(int i = 0):a(i) { } ~A() { cout<<"in a destructor"<<endl; }};class B { A obj[3]; double *pb[10];public: B(int k) { cout<<"in b constructor"<<endl; for(int i = 0; i < 10; i++) { pb[i] = new double[20000000]; if(pb[i] == 0) { throw i; } else { cout<<"allocated 20000000 doubles in pb["<<i<<"]"<<endl; } } }};int main() { try { B b(2); } catch (int e) { cout<<"catch an exception when allocated pb["<<e<<"]"<<endl; } return 0;}
异常类
#include<bits/stdc++.h>using namespace std;/** * 异常类 * */const int MAX = 3;class Full {};class Empty {};class Stack { int s[MAX]; int top;public: void push(int a) { if(top >= MAX-1) { throw Full(); } s[++top] = a; } int pop() { if(top < 0) { throw Empty(); } return s[top--]; } Stack() { top = -1; }};int main() { Stack s; try { s.push(10); s.push(20); s.push(30); // s.push(40); // 引起栈满异常 cout<<s.pop()<<endl; cout<<s.pop()<<endl; cout<<s.pop()<<endl; cout<<s.pop()<<endl; } catch(Full) { cout<<"Exception Stack Full"<<endl; } catch(Empty) { cout<<"Exception Stack Empty"<<endl; } return 0;}
异常对象
#include<bits/stdc++.h>using namespace std;/** * 异常对象 * */const int MAX = 3;class Full { int a;public: Full(int i):a(i) { } int getValue() { return a; }};class Empty {};class Stack { int s[MAX]; int top;public: Stack() { top = -1; } void push(int a) { if(top >= MAX-1) { throw Full(a); } s[++top] = a; } int pop() { if(top < 0) { throw Empty(); } return s[top--]; }};int main() { Stack s; try { s.push(10); s.push(20); s.push(30); s.push(40); } catch (Full e) { cout<<"Exception:Stack Full"<<endl; cout<<"The Value not push in stack:"<<e.getValue()<<endl; } return 0;}
派生异常类
#include<bits/stdc++.h>using namespace std;/** * 派生异常类 * */class BasicException {public: char *Where() { return "BasicException"; }};class FileSysException: public BasicException {public: char *Where() { return "FileSysException"; }};class FileNotFound: public FileSysException {public: char *Where() { return "FileNotFound"; }};class DiskNotFound: public FileSysException {public: char * Where() { return "DiskNotFound"; }};int main() { try { throw FileSysException(); } catch(DiskNotFound p) { cout<<p.Where()<<endl; } catch(FileNotFound p) { cout<<p.Where()<<endl; } catch(FileSysException p) { cout<<p.Where()<<endl; } catch(BasicException p) { cout<<p.Where()<<endl; } try { throw DiskNotFound(); } catch(BasicException p) { cout<<p.Where()<<endl; } catch(FileSysException p) { cout<<p.Where()<<endl; } catch(DiskNotFound p) { cout<<p.Where()<<endl; } catch(FileNotFound p) { cout<<p.Where()<<endl; } return 0;}
多态捕获派生类异常
#include<bits/stdc++.h>using namespace std;/** * 多态捕获派生异常类 * */class BasicException {public: virtual char *Where() { return "BasicException"; }};class FileSysException: public BasicException {public: char *Where() { return "FileSysException"; }};class FileNotFound: public FileSysException {public: char *Where() { return "FileNotFound"; }};class DiskNotFound: public FileSysException {public: char * Where() { return "DiskNotFound"; }};int main() { try { throw FileSysException(); } catch(BasicException &p) { cout<<p.Where()<<endl; } try { throw DiskNotFound(); } catch(BasicException &p) { // 注意这里必须加引用才能体现多态和虚函数的性质,否则会调用基类的异常处理 cout<<p.Where()<<endl; } return 0;}