Wednesday, December 30, 2009

Exceptions

Exception
is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions
When an exception occurs, an exception object, is created which consists of information about the error. This process of creating an exception object during runtime is called throwing an exception. An appropriate exception handler is chosen to handle the exception. This is called catching an exception

Any Java programming language code must honor the Catch or Specify Requirement. That is, a set of instructions that can throw exceptions must by follow either of the following two rules:
  • A try statement that catches the exception. The try must provide a handler for the exception
  • A method that specifies that it can throw the exception. The method must provide a throws clause that lists the exception.

Kinds of Exceptions

  1. Check Exceptions: These are exceptional conditions that a well-written application should anticipate and recover from. For example, suppose the application accepts an array of integers from the commandline, but if the number of integers exceed the array size limit defined in the program then the application should prompt the user to re-enter correct number of integers. Checked Exceptions are subject to Catch or Specify requirement while other exceptions do not comply.
    RuntimeException, and their subclases.
  2. Error: These are conditions that occur external to the application. Hardware failures is the best example of such conditions.
  3. Runtime Exception: These are internal application faults that the application cannot anticipate. These usually indicate programming bugs. Rather than catching these exceptions and handling them it is better to eliminate the bug that caused the exception to occur.

try, catch and finally blocks

try block
it contains the code that might throw an exception
. try block can enlcose a single line of code that might throw an exception or a full block of method.
catch block
it contains the code required to handle an exception thrown from a try block.
Multiple catch blocks can exist based on the number exception thrown from a try block. The first catch block must immediately follow the try block.
finally block
it contains code, that always executes when the try block exits.
The finally block is very useful to cleanup used resources for furthur re-use. A catch is only an option to specify when a finally block is specified

Specifying Exceptions with the throws Clause

If we do not want to specify a catch block(s) for the exceptions occurring in a method, but would like some method higher in the call stack to handle it, then we can just specify the type of exceptions the method throws in the method declaration itself.
Example:
   public void writeList() throws IOException, ArrayIndexOutOfBoundsException {
Since, ArrayIndexOutOfBoundsException is an unchecked exception, including it in the throws clause is not mandatory.

throw Statement

Inorder, to catch an exception object, it should be thrown at the first place. The throw keyword is used by methods, in this context, to throw exception objects.
Example:
   if(size == 0){
      throw new EmptyStackException();
   }
The above example, is a code taken partly from the pop() method of common stack object. Here, if the size of the stack is empty it throws an object of EmptyStackException() exception class.

Throwable Class

All exception-objects are objects of Throwable class or its subclasses. Error and Exception are the first descendants of throwable class.

Chained Exception

An exception can throw another exception, hence forming a chain of exceptions. This is possible, in general by throwing an exception inside a catch block. The following methods and constructors in Throwable support chained exceptions:
  • Throwable getCause()
  • Throwable initCause(Throwable)
  • Throwable(String, Throwable)
  • Throwable(Throwable)
The Throwable arguement to initCause() method the Throwable constructors is the exception that caused the current exception. getCause() method returns the exception that caused the current exception.

Stack Trace Information
Stack Trace
it provides information on the execution history of the current thread and lists the names of the classes and methods that were called at the point when the exception occurred.
Using the getStackTrace() method on the exception object will allow you to know the source of exception chain.

Creating Exception Classes

The Java Platform, provides a wide range of exception classes for use. If for any purpose, you require your own exception class
related to the program then you can manually create. All exception-classes can inherit any of the subclasses of Exception class. But it would be more appropriate to inherit Exception only.


tutorials

No comments:

Post a Comment