A Java exception is an object that describes an exceptional condition which has occurred in a piece of code. Many things can lead to exceptions , including hardware failures, resource exhaustion and bugs in the program. When an exceptional event occurs in the Java program , an exception is thrown. The code that is responsible for doing something about the exception is called “exception handler”.
The Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. We have tell the JVM what code to execute when certain exceptional condition occurs in the program. To do this we use try and catch keywords. Within a try block we will write a code in which exception may occur. This block of code is called guarded region. One or more catch clauses match a specific exception to a block of code that handles it.
Here is the basic form of exception handling block.
try{
//block of code
}catch ( ExceptionType1 ex1){
// exception handler for ExcpetionType1.
} catch(ExceptionType2 ex2){
//Exception handler for ExcpetionType2.
throw(e);
}finally{
}
Java Exception Type Hierarchy:.
· Throwable class is used represent all exceptional conditions. This is superclass of all Exception types.
· Two immediate subclass of Throwable class is Error and Exception.
· Exception is used for exceptional conditions that user programs should catch.
· Exception class is used to create our own custom exception. Just we have to create a subclass of Exception and we create our exceptional conditions in this class that user program to catch.
· Example for Exception Class
public class Propagate {
public static void main(String[] args) {
String str="pramila";
Propagate p=new Propagate();
try{
String revStr=p.reverse(str);
System.out.println(revStr);
}catch(StringIndexOutOfBoundsException stEx){
System.out.println("Exception"+stEx.getMessage());
}catch(IndexOutOfBoundsException ie){
System.out.println("Exception"+ie.getMessage());
}finally{
System.out.println("Exception occured in finally block");
try{
System.out.println(" In finally block try block Value"+5/0);
}catch(ArithmeticException ie){
System.out.println("Exception occured"+ie.getMessage());
}
}
}
public String reverse(String str){
String reveString = "";
for(int i=str.length();i>0;i--){
reveString += str.charAt(i);
}
return reveString;
}
}
· Throw statement is used to explicitly throw an exception. First we have to get a handle on an instance of Throwable, via a parameter in to a catch clause or by creating one using new operator. General form of throw statement is throw ThrowableInstance;
· Throws keyword is used to identify the list of possible exceptions that a method might throw. General form of throws statement is type method-name(arg-list) throws exception-list {}
· Sometimes it is necessary to be assured that given piece of code will run no matter which exceptions are caused and caught. The finally keyword can be used to identify such a block of code.
· It is illegal to use a try clause without either a catch or finally clause.
· There are two types of exceptions
Checked Exception: - Checked exception must be explicitly caught or propagated. It extends the class java.lang.Exception
Unchecked Exception: Is not explicitly caught or propagated. It extends the the class java.lang.RuntimeException
· Error defines the conditions that should not be expected to be caught under normal circumstances. Error are typically created in response to catastrophic failures. For Example StackoverflowError. The most common way this to occur is to create a recursive method. Look at the following code
void go(){
go();
}
Here if we make a mistake of invoking the go() method, our program will fall into a black hole; go invoking go() invoking go, until, no matter how much memory we have, we will get a StackOverflowError. Here only JVM knows when this event occurs, and the JVM will be the source of this error.