Friday, August 22, 2008

==> Exception Handling in Java

Exception can be generated by Java-runtime system or they can be manually generated by code.

Error-Handling becomes a necessary while developing an application to account for exceptional situations that may occur during the program execution, such as
Run out of memory,Resource allocation Error,Inability to find a file and etc.If the Resource file is not present in the disk, you can use the Exception handling mechanisim to handle such abrupt termination of program.

Exception class is used for the exceptional conditions that are trapped by the program.An exception is an abnormal condition or error that occur during the execution of the program.

Error the error class defines the conditions that do not occur under normal conditions.

Eg: Run out of memory, Stack overflow error.

Two types of exceptions:

1. Checked Exceptions : must be declare in the method declaration or caught in a catch block. Checked exception must be handled at Compile Time. Environmental error that cannot necessarly be detected by Testing, Eg: disk full, brocken Socket, Database unavailable etc.

2. Un-checked Exceptions: Run-time Exceptions and Error, does’t have to be declare.(but can be caught).

Run-time Exceptions : programming errors that should be detectd in Testing ,
Arithmetic, Null pointer, ArrayIndexOutofBounds, ArrayStore, FilenotFound, NumberFormate, IO, OutofMemory.

Errors: Virtual mechine error – class not found , out of memory, no such method , illegal access to private field , etc.

Java Exception handling can be managed by five keywords:

Try : The try block governs the statements that are enclosed within it and defines the scope of exception handler associated with it. Try block follows catch or finally or both.

Catch: This is a default exception handler. since the exception class is the base class for all the exception class, this handler id capable of catching any type of exception.
The catch statement takes an Object of exception class as a parameter, if an exception is thrown the statement in the catch block is executed. The catch block is restricted to the statements in the proceeding try block only.

Try {
// statements that may cause exception
}
catch(Exception obj)
{

}


Finally : when an exception is raised, the statement in the try block is ignored, some times it is necessary to process certain statements irrespective of wheather an exception is raised or not, the finally block is used for this purpose.

Throw : The throw class is used to call exception explicitly. You may want to throw an exception when the user enters a wrong login ID and pass word, you can use throw statement to do so.

The throw statement takes an single argument, which is an Object of exception class.


Throw [throwable Instance]


If the Object does not belong to a valid exception class the compiler gives error.

Throws :The throws statement species the list of exception that has thrown by a method.
If a method is capable of raising an exception that is does not handle, it must specify the exception has to be handle by the calling method, this is done by using the throw statement.

[access specifier] [access modifier] {return type} {method name} {arg-list} [exception-list]


Eg: public void accept password( ) throws illegalException
{
System.out.println(“Intruder”);
Throw new illegalAccesException;
}

No comments: