home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1603.CPP
-
- //----------------------------------------------------------
- // Exception handling is supported only by
- // the IBM C Set++ Compiler
- // This example does not work in Borland Turbo C++
- // or in Microsoft Visual C++
- //----------------------------------------------------------
- // To run this program on an OS/2 machine:
- // move to an OS/2 window
- // [type] cd:\EXAMPLES\EX1603I
- // where cd: is the drive of the CD-ROM
- //----------------------------------------------------------
-
- #include <iostream.h>
-
- //
- // This is example shows the various forms of catch declarations
- //
- void Process( int i)
- {
- try
- {
- if (i == 1)
- {
- throw i; // throw an integer
- }
- else if (i ==2)
- {
- throw (float)i; // throw a float
- }
- else if (i == 3)
- {
- throw double(i); // throw a double
- }
- }
- catch( int y) // catching an expression
- {
- if (y >= 0)
- {
- cout << "Exception handler: catch( int y){ ...}"
- << endl
- << "A integer exception greater than zero caught,"
- << " its value is: "
- << y << endl << endl;
- }
- else
- {
- cout << "A integer exception less than zero was caught,"
- << " its value is: "
- << y << endl;
- }
- }
- catch( float) // catching a type
- {
- cout << "Exception handler: catch( float){ ...}" << endl
- << "A floating point exception was caught."
- << " Its value is unknown "
-
-
- << endl << endl;
- throw;
- }
- catch(...) // catching an exception of unknown type
- {
- cout << "Exception handler: catch( ...){ ...}" << endl
- << "An exception was caught, "
- << "but there is no way of knowing the type. "
- << endl << endl;
- throw;
- }
- }
-
-
- void main()
- {
- //
- // Each iteration of the loop causes the invocation
- // of an exception handler
- // using a different catch declaration
- //
- for (int i = 1; i <= 3; i++) {
- try {
- Process( i);
- }
- catch (...)
- {
- cout << "The rethrown object was caught. "
- << endl << endl;
- }
- }
- }
-