异常处理结构

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 异常处理
  5. * 注意:类型必须完全匹配
  6. * */
  7. int main() {
  8. cout<<"1--before try block"<<endl;
  9. try {
  10. cout<<"2--inside try block"<<endl;
  11. throw 10;
  12. cout<<"3--after throw"<<endl;
  13. }
  14. catch(int i) {
  15. cout<<"4--in catch block 1 errcode is"<<i<<endl;
  16. }
  17. catch(char *s) {
  18. cout<<"5-in catch block 2 errcode is"<<s<<endl;
  19. }
  20. cout<<"6--after catch"<<endl;
  21. return 0;
  22. }

在函数中处理异常

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 在函数中处理异常
  5. * */
  6. void temperature(int t) {
  7. if(t == 100) {
  8. throw "hot";
  9. }
  10. else if(t == 0) {
  11. throw "cold";
  12. }
  13. else {
  14. cout<<"temperature is: "<<t<<endl;
  15. }
  16. }
  17. int main() {
  18. try {
  19. temperature(0);
  20. temperature(10);
  21. temperature(100);
  22. }
  23. catch(const char *s) {
  24. cout<<s<<endl;
  25. }
  26. return 0;
  27. }

捕获所有异常

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 捕获所有异常
  5. * */
  6. void errhandler(int n) throw() {
  7. try {
  8. if(n == 1) {
  9. throw n;
  10. }
  11. if(n == 2) {
  12. throw "dx";
  13. }
  14. if(n == 3) {
  15. throw 1.1;
  16. }
  17. }
  18. catch(...) {
  19. cout<<"catch an exception"<<endl;
  20. }
  21. }
  22. int main() {
  23. errhandler(1);
  24. errhandler(2);
  25. errhandler(3);
  26. return 0;
  27. }

嵌套异常捕获

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 嵌套异常捕获
  5. * */
  6. void fc( ){
  7. try{
  8. throw "help";
  9. }
  10. catch (int x) {
  11. cout<< "in fc int handler"<<endl;
  12. }
  13. try {
  14. cout<<"no error handler"<<endl;
  15. }
  16. catch (const char *px) {
  17. cout<<"in fc char * handler"<<endl;
  18. }
  19. }
  20. void fb() {
  21. int *q = new int(10);
  22. try {
  23. fc();
  24. cout<<"return form fc(0)"<<endl;
  25. }
  26. catch(...) {
  27. delete []q;
  28. throw;
  29. }
  30. }
  31. void fa() {
  32. char *p = new char[10];
  33. try {
  34. fb();
  35. cout<<"return form fb(0)"<<endl;
  36. }
  37. catch(...) {
  38. delete []p;
  39. throw;
  40. }
  41. }
  42. int main() {
  43. try {
  44. fa();
  45. cout<<"return form fa"<<endl;
  46. }
  47. catch(...) {
  48. cout<<"in main"<<endl;
  49. }
  50. cout<<"end"<<endl;
  51. return 0;
  52. }

构造函数异常

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 构造函数异常
  5. * */
  6. class A {
  7. int a;
  8. public:
  9. A(int i = 0):a(i) {
  10. }
  11. ~A() {
  12. cout<<"in a destructor"<<endl;
  13. }
  14. };
  15. class B {
  16. A obj[3];
  17. double *pb[10];
  18. public:
  19. B(int k) {
  20. cout<<"in b constructor"<<endl;
  21. for(int i = 0; i < 10; i++) {
  22. pb[i] = new double[20000000];
  23. if(pb[i] == 0) {
  24. throw i;
  25. }
  26. else {
  27. cout<<"allocated 20000000 doubles in pb["<<i<<"]"<<endl;
  28. }
  29. }
  30. }
  31. };
  32. int main() {
  33. try {
  34. B b(2);
  35. }
  36. catch (int e) {
  37. cout<<"catch an exception when allocated pb["<<e<<"]"<<endl;
  38. }
  39. return 0;
  40. }

异常类

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 异常类
  5. * */
  6. const int MAX = 3;
  7. class Full {
  8. };
  9. class Empty {
  10. };
  11. class Stack {
  12. int s[MAX];
  13. int top;
  14. public:
  15. void push(int a) {
  16. if(top >= MAX-1) {
  17. throw Full();
  18. }
  19. s[++top] = a;
  20. }
  21. int pop() {
  22. if(top < 0) {
  23. throw Empty();
  24. }
  25. return s[top--];
  26. }
  27. Stack() {
  28. top = -1;
  29. }
  30. };
  31. int main() {
  32. Stack s;
  33. try {
  34. s.push(10);
  35. s.push(20);
  36. s.push(30);
  37. // s.push(40); // 引起栈满异常
  38. cout<<s.pop()<<endl;
  39. cout<<s.pop()<<endl;
  40. cout<<s.pop()<<endl;
  41. cout<<s.pop()<<endl;
  42. }
  43. catch(Full) {
  44. cout<<"Exception Stack Full"<<endl;
  45. }
  46. catch(Empty) {
  47. cout<<"Exception Stack Empty"<<endl;
  48. }
  49. return 0;
  50. }

异常对象

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 异常对象
  5. * */
  6. const int MAX = 3;
  7. class Full {
  8. int a;
  9. public:
  10. Full(int i):a(i) {
  11. }
  12. int getValue() {
  13. return a;
  14. }
  15. };
  16. class Empty {
  17. };
  18. class Stack {
  19. int s[MAX];
  20. int top;
  21. public:
  22. Stack() {
  23. top = -1;
  24. }
  25. void push(int a) {
  26. if(top >= MAX-1) {
  27. throw Full(a);
  28. }
  29. s[++top] = a;
  30. }
  31. int pop() {
  32. if(top < 0) {
  33. throw Empty();
  34. }
  35. return s[top--];
  36. }
  37. };
  38. int main() {
  39. Stack s;
  40. try {
  41. s.push(10);
  42. s.push(20);
  43. s.push(30);
  44. s.push(40);
  45. }
  46. catch (Full e) {
  47. cout<<"Exception:Stack Full"<<endl;
  48. cout<<"The Value not push in stack:"<<e.getValue()<<endl;
  49. }
  50. return 0;
  51. }

派生异常类

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 派生异常类
  5. * */
  6. class BasicException {
  7. public:
  8. char *Where() {
  9. return "BasicException";
  10. }
  11. };
  12. class FileSysException: public BasicException {
  13. public:
  14. char *Where() {
  15. return "FileSysException";
  16. }
  17. };
  18. class FileNotFound: public FileSysException {
  19. public:
  20. char *Where() {
  21. return "FileNotFound";
  22. }
  23. };
  24. class DiskNotFound: public FileSysException {
  25. public:
  26. char * Where() {
  27. return "DiskNotFound";
  28. }
  29. };
  30. int main() {
  31. try {
  32. throw FileSysException();
  33. }
  34. catch(DiskNotFound p) {
  35. cout<<p.Where()<<endl;
  36. }
  37. catch(FileNotFound p) {
  38. cout<<p.Where()<<endl;
  39. }
  40. catch(FileSysException p) {
  41. cout<<p.Where()<<endl;
  42. }
  43. catch(BasicException p) {
  44. cout<<p.Where()<<endl;
  45. }
  46. try {
  47. throw DiskNotFound();
  48. }
  49. catch(BasicException p) {
  50. cout<<p.Where()<<endl;
  51. }
  52. catch(FileSysException p) {
  53. cout<<p.Where()<<endl;
  54. }
  55. catch(DiskNotFound p) {
  56. cout<<p.Where()<<endl;
  57. }
  58. catch(FileNotFound p) {
  59. cout<<p.Where()<<endl;
  60. }
  61. return 0;
  62. }

多态捕获派生类异常

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4. * 多态捕获派生异常类
  5. * */
  6. class BasicException {
  7. public:
  8. virtual char *Where() {
  9. return "BasicException";
  10. }
  11. };
  12. class FileSysException: public BasicException {
  13. public:
  14. char *Where() {
  15. return "FileSysException";
  16. }
  17. };
  18. class FileNotFound: public FileSysException {
  19. public:
  20. char *Where() {
  21. return "FileNotFound";
  22. }
  23. };
  24. class DiskNotFound: public FileSysException {
  25. public:
  26. char * Where() {
  27. return "DiskNotFound";
  28. }
  29. };
  30. int main() {
  31. try {
  32. throw FileSysException();
  33. }
  34. catch(BasicException &p) {
  35. cout<<p.Where()<<endl;
  36. }
  37. try {
  38. throw DiskNotFound();
  39. }
  40. catch(BasicException &p) { // 注意这里必须加引用才能体现多态和虚函数的性质,否则会调用基类的异常处理
  41. cout<<p.Where()<<endl;
  42. }
  43. return 0;
  44. }