Exception handling is a powerful mechanism in java programming. Exception is a problem in the program of
an application at run time and exception handling is a technique or mechanism to maintain the normal flow of the application.Directly we can point exception as a abnormal behavior of a program or an application. The exception could be at compile time or at run time, both can divide in the type of the exception.
These are type of exception
{
//body of catch block
}
finally
{
System.out.println("this is finally block");
}
catch have an parameter and the name of the exception same as the given example.multiple catch block can exist in a class for different type of exception.
Exception and Sub Classes of Exception Class
These are type of exception
- Checked Exception The checked exception is a compile time exception and the class with checked exception extends Throwable class except RuntimeException and the exceptions are IOException and SQLException etc.An example considers as, when we want to open a file in our program and that file is not exist then it is IOException.
- Unchecked Exception The exceptions which check at run time call the RuntimeException or checked Exception.In these we have fore types.
- ArrayIndexOutOfBoundException Ex.-- int arr[]=new int[3];
- ArithmeticException Ex.-- a=4/0; // ArithmeticException
- NullPointerException Ex.-- String str=null;
- NumberFormatException Ex.-- String str='g';
- Error The errors are the basically mistakes by the programmers and could be the situation which can not be recoverable.Like mistyping the name of a method or variable name, is example of programmer mistake, OutOfMemoryError by the over flow of Stack is another type which is not recoverable but these are ignored at compile time.
arr[6]=13; // ArrayIndexOutOfBound
- System.out.println(str.length()); // NullPointerException
- int a=Integer.pareseInt(str); // NumberFormatException
try-catch and finally blocks in exception handling
In the try block we put the code which could generate an Exception in the program.The try block is a enclosing block of the suspected code for Exception generate or the code which can generate an Exception but try block should be with the catch or with the finally and catch both blocks to handle an Exception.
The catch block must use after to the try block to handle the Exception with the name of the Exception.In this series the finally block is optional.The finally block if in the it always executes.The finally block use normally for the cleaning up the code like close the database connections,closing the the file and close the network connections which should be close after the work.
here is a simple example of these blockes.
int a;
try{
a=4/0; // ArithmeticException
}
catch(ArithmeticException e1){
//body of catch block
}
finally
{
System.out.println("this is finally block");
}
catch have an parameter and the name of the exception same as the given example.multiple catch block can exist in a class for different type of exception.