Java Exceptions

 

·                   An exception, or exceptional condition, is an occurrence during your program execution that alters the normal flow.

·                   An Exception object is an instance of the class Exception or one of its subclasses.

·                   An Error object is an instance of the class Error or one of its subclasses.

·                   A section of code enclosed in braces and started with the keyword try is referred to as a guarded region.  The try keyword governs this region.  This is th code that will be watched for exceptions.

·                   The catch keyword is used to define an exception handler for a particular type of exception object and its subclasses, if any.

·                   Catch blocks must always immediately follow the try block they pertain to and the other catch blocks for the same try block.  Catch blocks are not required.

·                   The appropriate catch block is executed immediately after an exception is thrown.  The remaining code in the try block is not executed.

·                   The finally keyword is used to define a block of code that is always executed after the catch block or after the try block if no exceptions occurred.  This block is typically used to release resources and to perform any needed cleanup for the try clause.  Finally blocks are not required.

·                   Either at least one catch block or a finally block must be specified with a try block.

·                   Any method from which an exception can be thrown must either handle this exception by providing a try block or else specify that the exception can be thrown.  This rule is referred to as the catch or specify requirement.

·                   Specifying a thrown exception is done by using the throws keyword after a method's parameter list and following it with a list of exception types.

·                   A runtime exception is an instance of the class RuntimeException or any of its subclasses.

·                   Runtime exceptions and Errors are exceptions to the catch or specify requirement.  Runtime exceptions may be thrown from any method without being handled or specified.

·                   A checked exception is any exception that is neither a RuntimeException nor and Error.  Checked exceptions are subject to the catch or specify requirement.  If checked exceptions are neither handled nor specified, the Java compiler will produce an error, and the application will not be compiled.

·                   Exceptions are thrown by instantiating an Exception class and using the throw keyword.

·                   You can create your own exceptions by subclassing any existing Exception class or any of its subclasses.

·                   An exception can output a stack trace to its origin point through its printStackTrace() method.

·                   Once caught, an exception may be re-thrown.  A re-thrown exception will contain the same stack trace information as it did when it was originally thrown.  You can modify the stack trace by calling an Exception object's fillInStackTrace method.  This method returns an object of type Throwable.