Exception Definitions

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 9.  Exception Handling


As you can see from the verbiage in the last section, there are a lot of terms associated with exception handling that need to be defined. The following is a list of exception terms and definitions:

  • Exceptions An exception is a representation of an error condition, or any situation that is not the expected result of a method.

  • Throwing an Exception The act of detecting an abnormal condition and generating an exception is called throwing an exception.

  • Catching an Exception When an exception is thrown it can be caught by a handler. This is referred to as catching an exception.

  • Handling an Exception The entire process is called handling the exception.

Try Blocks

A try block is a Java programming language construct that encloses one or more statements that can throw an exception; it uses the keyword try. The try block starts with the try keyword, and then has a body (enclosed by braces) that contains code that can generate an exception. The general form is

 try {     // Some code in here can throw an exception!  } 

Any method that can throw an exception must be called within the context of a try block.

Catch Blocks

Exceptions that are thrown are caught inside catch blocks. A try block can be followed by zero or more catch blocks (if there are zero then there must be a finally block see the next section), each catching and handling a different exception; catch blocks use the keyword catch. The catch block starts with the catch keyword followed by a parentheses-delimited, exception-class instance variable (that was thrown by the offending method), and, finally, a brace-delimited body that handles the exception. The general form is

 catch( ExceptionClassName exception ) {     // Code to get information out of e and handle the exception  } 

exception is an instance of the exception class ExceptionClassName; it has methods and attributes that you can access just like any other class. It should, however, give you insight to the cause of the error condition.

Finally Block

A catch or try block can be followed by a single finally block. The compiler always executes the statements inside a finally block regardless of whether an exception is thrown and caught or never thrown. The finally block follows the try block if there are no catch blocks, otherwise, it follows the last catch block. finally blocks are denoted by the keyword finally, followed by a brace-delimited body of code to execute.

The purpose of the finally block is to define code that you want executed regardless of whether the code in the try block succeeds or fails. One of the most practical applications of this is with regard to resource management; if you allocate a resource in your try block, you want to release it if your code succeeds or fails. It has the general form:

 try {  }  catch( ... ) {  }  finally {     // Release your resources  } 

       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net