home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1604.CPP
- //
- // A complete example of exception handling
- //
- //----------------------------------------------------------
- // 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\EX1604I
- // where cd: is the drive of the CD-ROM
- //----------------------------------------------------------
-
- #include <iostream.h>
-
- //
- // 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);
- } else {
- return(x/y);
- }
- }
-
- //
- // FUNCTION: Process(...) does the work
- //
- void Process()
- {
- long z = divide( 1, 0); // call divide
- }
-
- void main()
- {
- //
- // Try block, catches exception thrown in Process' call chain
- //
- try
- {
- Process();
- }
- catch( long x) {
- cout << "Exception handler: catch( long x)" << endl
- << "The exception value is: " << x << endl;
- }
- catch( ...) {
- cout << "Caught something" << endl;
- }
- }
-