Advertisement
fueanta

Exception Handing [restricted throw]

Sep 6th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. // Throw restrictions on an exception handling function
  2. // practice...
  3. #include <iostream>
  4. #include <conio.h>
  5. #include <stdlib.h>
  6. #include <exception>
  7.  
  8. using namespace std;
  9.  
  10. void mr_terminator(void) {
  11.     cout << "Exception could not be handled." << endl;
  12.     exit(0);
  13. }
  14.  
  15. void test(int x) throw(int,double) {
  16.     if (x == 0) throw 'x';  //char
  17.     else if (x == 1) throw x;  //int
  18.     else if (x == -1) throw 1.0; // double
  19.     cout << "Exception could not be found." << endl;
  20. }
  21.  
  22. int main(void) {
  23.     set_terminate(mr_terminator);
  24.     try {
  25.         cout << "Invoking Restricted Throw Function." << endl;
  26.         cout << "x == 0" << endl;
  27.         test(0);
  28.         cout << "x == 1" << endl;
  29.         test(1);
  30.         cout << "x == -1" << endl;
  31.         test(-1);
  32.         cout << "x == 2" << endl;
  33.         test(2);
  34.     }
  35.     catch (char c) {
  36.         cout << "Caught a character." << endl;
  37.     }
  38.     catch (int m) {
  39.         cout << "Caught an integer." << endl;
  40.     }
  41.     catch (double d) {
  42.         cout << "Caught a double." << endl;
  43.     }
  44.     catch (...) {
  45.         cout << "Caught an exception." << endl;
  46.     }
  47.     cout << "End of the Program." << endl;
  48.     getch();
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement