C++ Notes: Exception Test

The following program illustrates exception handling.

Source program

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 
// exceptions/exception-test.cpp - Test exceptions.
// 2004-02-10 - Fred Swartz

//==================================== includes
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;

//================================================ prototypes
void generateException(int whichException);

//====================================================== main
int main() {
    for (int i=1; ; i++) {  // Loop forever
        try {
            cout << i;
            generateException(i);
        } catch (out_of_range orex) {
            cout << "    Catching: out_of_range "<< orex.what() << endl;
        } catch (const char* mess) {
            cout << "    Catching: const char* " << mess << endl;
        } catch (string smess) {
            cout << "    Catching: string " << smess << endl;
        } catch (int iex) {
            cout << "    Catching: int " << iex << endl;
        } catch (runtime_error rex) {
            cout << "    Catching: runtime_error " << rex.what() << endl;
        } catch (exception eex) {
            cout << "    Catching: " << eex.what() << endl;
        } catch (...) {
            cout << "    ERROR: Nobody caught this!" << endl;
        }
    }            

    system("PAUSE");   // Keep Dev-C++ window open
    return 0;
}

//========================================= generateException
void generateException(int whichException) {
    switch (whichException) {
        
    case 1:
        cout << "  Throwing out_of_range()" << endl;
        throw out_of_range("out_of_range meaningful comment");
        break;
        
    case 2:
        cout << "  Throwing exception()   // Can't specify comment" << endl;
        throw exception();  // Doesn't take comment text.
        break;
        
    case 3:
        cout << "  Throwing underflow_error  // caught by base class (runtime_error)" << endl;
        throw underflow_error("underflow_error");
        break;
        
    case 4:
        cout << "  Throwing runtime_error" << endl;
        throw runtime_error("a comment");
        break;
        
    case 5:
        cout << "  Throwing length_error   // caught be super-super-class (exception)" << endl;
        throw length_error("length_error");
        break;
        
    case 6:
        cout << "  Throwing int" << endl;
        throw 26;
        break;
        
    case 7:
        cout << "  Throwing const char*" << endl;
        throw "This is a const char*";
        break;
        
    case 8:
        cout << "  Throwing string" << endl;
        throw string("I'm a string");
        break;
        
    case 9:
        cout << "  Throwing float" << endl;
        throw 3.14159;
        break;
        
    default:
        cout << "  Throwing up" << endl;
        system("PAUSE");
        exit(0);
    }
    return;

}

Output

The following output is from Dev-C++ (using gcc version ?). MS Visual C++ v6 gives a similar result, differing only in what catching exception prints.

1  Throwing out_of_range()
    Catching: out_of_range out_of_range meaningful comment
2  Throwing exception()   // Can't specify comment
    Catching: St9exception
3  Throwing underflow_error  // caught by base class (runtime_error)
    Catching: runtime_error underflow_error
4  Throwing runtime_error
    Catching: runtime_error a comment
5  Throwing length_error   // caught be super-super-class (exception)
    Catching: St9exception
6  Throwing int
    Catching: int 26
7  Throwing const char*
    Catching: const char* This is a const char*
8  Throwing string
    Catching: string I'm a string
9  Throwing float
    ERROR: Nobody caught this!
10  Throwing up