home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1606.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\EX1606I
- // where cd: is the drive of the CD-ROM
- //----------------------------------------------------------
-
- #include <iostream.h>
- #include <string.h>
-
- //
- // This is example shows how define an exception
- //
-
- typedef char String32[33]; // Used as a generic string
-
- //
- // Exception
- //
- class MathExcept {
- String32 sName;
- public:
- MathExcept() { strcpy( (char *)sName, "Math Exception"); }
-
- char*
- Name() { return (char *)sName; }
- };
-
-
- //
- // divide: returns x divided by y
- // throws an object of class Exception on error
- //
- long divide( long x, long y)
- {
- if (y==0)
- {
- throw ( MathExcept());
- }
- return ( x/y);
- }
-
- //
- // main calls divide and catches exceptions of class exception
- //
- main()
- {
- try
- {
- long z = divide( 1, 0);
- cout << "1 divided by 0 is " << z << endl;
- }
- catch( MathExcept except)
- {
- cout << "Exception handler: catch( MathExcept except)"
- << endl
- << "I caught a " << except.Name()
- << " exception.";
- }
- }
-