home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C23 / Except.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  936 b   |  47 lines

  1. //: C23:Except.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Basic exceptions
  7. // Exception specifications & unexpected()
  8. #include <exception>
  9. #include <iostream>
  10. #include <cstdlib>
  11. #include <cstring>
  12. using namespace std;
  13.  
  14. class Up {};
  15. class Fit {};
  16. void g();
  17.  
  18. void f(int i) throw (Up, Fit) {
  19.   switch(i) {
  20.     case 1: throw Up();
  21.     case 2: throw Fit();
  22.   }
  23.   g();
  24. }
  25.  
  26. // void g() {} // Version 1
  27. void g() { throw 47; } // Version 2
  28. // (Can throw built-in types)
  29.  
  30. void my_unexpected() {
  31.   cout << "unexpected exception thrown";
  32.   exit(1);
  33. }
  34.  
  35. int main() {
  36.   set_unexpected(my_unexpected);
  37.   // (ignores return value)
  38.   for(int i = 1; i <=3; i++)
  39.     try {
  40.       f(i);
  41.     } catch(Up) {
  42.       cout << "Up caught" << endl;
  43.     } catch(Fit) {
  44.       cout << "Fit caught" << endl;
  45.     }
  46. } ///:~
  47.