Generating Exceptions


Java provides powerful exception handling. Any method can be "throw" an exception. They are handled using the "try, catch, finally" construct.

The following code fragment shows how to declare that a method throws an exception, and then to actually throw one:

   public ComplexFloat divideBy( ComplexFloat divisor )
      throws DivideByZeroException;
   {
      ...
      throw new DivideByZeroException( );
      ...
   }
Remember that an exception, like everything else in Java, is an object - thus we have to use 'new' to create one.