home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1612.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\EX1612I
- // where cd: is the drive of the CD-ROM
- //----------------------------------------------------------
-
- #include <iostream.h>
-
- class MathErr { }; // Used to throw Math related exceptions
-
- //
- // FUNCTION: divide(...) divides first parameter by the second
- // PARAMETERS: long x dividend
- // long y divisor
- // RETURN VALUE: x divided by y
- //
- long divide(long x, long y) throw ( long)
- {
- if (y==0)
- {
- throw y; // can't divide by zero so throw an exception
- }
- return x/y; // return the quotient
- }
-
-
- //
- // FUNCTION: Process does the work
- // EXCEPTION SPEC: does not throw any exceptions
- //
- void Process() throw()
- {
- long z = divide( 1, 0); // attempt to divide one by zero
- }
-
- void main()
- {
- try
- {
- cout << "Calling Process()" << endl;
- Process();
- }
- catch(long x) // unexpected is called instead
- {
- cout << "An unexpected exception was thrown, "
- << "these lines won't be executed.";
- }
- }
-