home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1605.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\EX1605I
- // where cd: is the drive of the CD-ROM
- //----------------------------------------------------------
-
- #include <iostream.h>
-
- //
- // This example shows how to rethrow an exception
- //
-
- void Process();
- long divide(long, long);
-
- main()
- {
- //
- // Try block, catches exception thrown in Process' call chain
- //
- try
- {
- Process(); // call the function to do the work
- }
- catch( long z) // catch the floating point exception
- {
- cout << "A long exception was caught, "
- << "its value was: " << z << endl;
- }
- }
-
- //
- // FUNCTION: Process(...) does the work
- //
- void Process()
- {
- try
- {
- int x, y;
- cout << "Enter two numbers: ";
- cin >> x >> y; // read in two numbers
- cout << endl << x << " divided by " << y << " is: "
- << divide(x, y); // print the quotient
- }
- catch( long y) // catch the exception
- {
- cout << "A long exception was caught and re-thrown"
- << endl;
- throw; // rethrow
- }
- }
-
- //
- // FUNCTION: divide(...) divides first parameter by second
- // PARAMETERS: long x dividend
- // long y divisor
- // RETURN VALUE: x divided by y
- //
- long divide(long x, long y)
- {
- if (y==0)
- {
- throw y; // can't divide by zero so throw an exception
- }
- return x/y; // return the quotient
- }
-