EXCEPTION HANDLING


EXCEPTION HANDLING

It is very common to have errors. Errors are irrecoverable events that leads to complete halt and cannot be handled. Sometimes exceptional situations arises that can be handled through the code, such as bad input data with the potential to bomb the program. It is important to note that error and checked exceptions are two different things. Error and Exception are different classes in Java, but both inherits Throwable. Java uses a form of exception trapping called, naturally enough, exception handling.

 

Java allows every method an alternative exit path if it is unable to complete its task in a normal way. In this situation, the method does not return a value. Instead it throws an object that encapsulates the exception information.

Execution does not resume at the code that called the method, instead exception handling mechanism begins its search for an exception handler that can deal with this particular exception condition.


CLASSIFICATION OF EXCEPTIONS



  • An exception object is always an instance of class derived from Throwable.
  • All exceptions descend from Throwable but hierarchy splits into two branches : Error and Exception.

 

1. Exceptions that inherit from RuntimeException include such problems as

  • A bad cast
  • An out-of-bounds array access
  • A null pointer access

2. Exceptions that do not inherit from RuntimeException include

  • Trying to read past the end of file
  • Trying to open a malformed URL
  • Trying to find a Class object for a string that does not denote an existing class

More often than not if there is RuntimeException, it might be your fault and you must solve it.

 

 

The Java language Specification calls any exception that derives from the class Error or class RuntimeException => unchecked exception

 

All other exceptions are called => checked exception

 

The compiler checks that you provide exception handlers for all checked exceptions.

 

CREATING AN EXCEPTION

Sometimes, our code has a problem which cannot be described by any of standard exception classes.In that case we will create our own class and inherit Exception class into it or from the child class of Exception like IOException. Now thing to remember we must create both default constructor and constructor that contains detailed message.

 

HOW TO THROW AN EXCEPTION

You need to decide what exception type to throw.

for eg: here End Of File has reached unexpectedly during input so we are throwing exception for it.

here is how you throw it :

throw new EOFException();

or

EOFException e = new EOFException();

throw e;

here is how it all fit together :

 

String readData( Scanner in ) throws EOFException

{

   while ( .... )

{   if ( ( ! in.hasNext() )   // EOF encountered

          { if ( n < len )

                     throw new EOFException();     }

}

    }


CATCHING EXCEPTIONS

 

  • catching exception means what to do after you have throw the exception.
  • to catch an exception, you set up a try / catch block.
  • Simplest form of try block : -

try

{    // code     }

catch ( ExceptionType e )

{    // handler for this type       }

 

If any of the code inside the try block throws an exception of the class specified in the catch clause, then

 

  • The program skips the remainder of the code in the try block.
  • The program executes the handler code inside the catch clause.
  • If none of the code inside the try block throws an exception, then the program skips the catch clause.



FINALLY

Whether an exception has caught or not, the code in the finally clause always get executed.

The keyword is "finally".

try

{          // statements          }

catch ( ExceptionType e )

{                //statements                         }

finally

{           //statements                   }