C++编程中异常处理实例代码

2014-11-24 13:10:53 · 作者: · 浏览: 0
  1. #include
  2. #include
  3. using namespace std;
  4. class Rainbow {
  5. public:
  6. Rainbow() {cout << "Rainbow()" << endl;}
  7. ~Rainbow() {cout << "~Rainbow()" << endl;}
  8. };
  9. void oz() {
  10. Rainbow rb;
  11. for(int i = 0; i < 3; i ) {
  12. cout << "theres no place like home" << endl;
  13. }
  14. throw 47;
  15. }
  16. int main()
  17. {
  18. try{
  19. cout << "tornado, witch, muchkins..." << endl;
  20. oz();
  21. }
  22. catch (int){
  23. cout << "Autie Em!"
  24. << " I had the strangest dream..."
  25. << endl;
  26. }
  27. return 0;
  28. }
  29. 输出:
  30. tornado, witch, muchkins...
  31. Rainbow()
  32. theres no place like home
  33. theres no place like home
  34. theres no place like home
  35. ~Rainbow()
  36. Autie Em! I had the strangest dream...
  37. Process returned 0 (0x0) execution time : 0.094 s
  38. Press any key to continue.