Exception Handling
A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code. When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error. That method may choose to handle the exception itself, or pass it on. Exceptions can be generated by the Java run-time system, or they can be manually generated by the code. Exceptions thrown by Java relate to fundamental errors that violate the rules of the Java language or the constraints of the Java execution environment. Manually generated exceptions are typically used to report some error condition to the caller of a method.
Java exception handling is managed via five keywords: try, catch, throw, throws, and finally.

Program statements that must be monitored for exceptions are contained within a try block. If an exception occurs within the try block, it is thrown. The code can catch this exception (using catch) and handle it in some rational manner. System-generated exceptions are automatically thrown by the Java run-time system. To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed before a method returns is put in a finally block.

Finally

finally creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally block will execute even if no catch statement matches the exception. Any time a method is about to return to the caller from inside a try/catch block, via an uncaught exception or an explicit return statement, the finally clause is also executed just before the method returns. This can be useful for closing file handles and freeing up any other resources that might have been allocated at the beginning of a method with the intent of disposing of them before returning. The finally clause is optional. However, each try statement requires at least one catch or a finally clause.

 

           
This is the general form of an exception-handling block:
try {

// block of code to monitor for errors

}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1

}
catch (ExceptionType2 exOb) {

// exception handler for ExceptionType2

}
finally {

// block of code to be executed before try block ends
}
If a finally block is associated with a try, the finally block will be executed upon
conclusion of the try.

Example for try and catch
                        The following program includes a try block and a catch clause which
processes the Arithmetic Exception generated by the division-by-zero error

           
class Exc2 {
public static void main (String args []) {
int d, a;
try {// monitor a block of code.
d = 0;
a = 42 / d;
System.out.println ("This will not be printed.");
} catch (Arithmetic Exception e) {// catch divide-by-zero error
System.out.println ("Division by zero.");
}
finally{}

System.out.println ("After catch statement.");
}
}

throws
A throws clause lists the types of exceptions that a method might throw. This is necessary for all exceptions, except those of type Error or Runtime Exception, or any of their subclasses. All other exceptions that a method can throw must be declared in the throws clause. If they are not, a compile-time error will result.

This is the general form of a method declaration that includes a throws clause:
type method-name (parameter-list) throws exception-list
{
// body of method
}

throw
It is possible for the gram to throw an exception explicitly, using the throw statement. The general form of throw is shown here:

throw ThrowableInstance;

The flow of execution stops immediately after the throw statement; any subsequent
statements are not executed.

A sample program that creates and throws an exception
class ThrowDemo {
static void demoproc() {
try {
throw new NullPointerException("demo");
}
catch(NullPointerException e) {
System.out.println("Caught inside demoproc.");
throw e; // rethrow the exception
}
}
public static void main(String args[]) {
try {
demoproc();
}
 catch(NullPointerException e) {
System.out.println("Recaught: " + e);
}
}

}