The process of catching the exception for converting JVM given exception message to end-user understandable message or for stopping abnormal termination of the program is called exception handling.
Basically by implementing Exception handling we are providing life to a program to talk to user on behalf of developer.
Java has following five keywords for throwing and catching exception:
Try keywords establishes a block to write a code that causes exceptions and its related statement. Exception causing statement must be placed in try block to handle and catch exception for shopping abnormal terminators and to display end-user understandable messages.
Catch block is used to catch exceptions those are thrown from its corresponding try block. It has logic to take necessary action on that caught exception.
Catch block syntax is looks like construction syntax. It does not take accessibility modifiers, normal modifiers, return type. IT takes only single parameter of type Throwable (or) its subclasses. Throwable is the super class of all exception classes.
Inside catch we can write any statement which is legal in java, including raising an exception.
Syntax of exception handlingtry { // block of code to monitor for errors } catch (ExceptionType1 exOb) { // exception handler for ExceptionType1 } catch (ExceptionType2 exOb) { // exception handler for ExceptionType2 } // ...
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ExceptionHandling { public static void main(String args[]){ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter your name : "); try { String input = reader.readLine(); System.out.println("You names is : "+input); // Exception prone area } catch (IOException e) { e.printStackTrace(); } } }Output
Enter your name : Ashish You names is : Ashish ...
finally establishes a block that definitely executes statements placed in it. Statement which are placed in finally block are always executed irrespective of the way the control is coming out from the try block either by completing normally or by return statement or by throwing exception by catching or not catching. 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.
Need of finally in real time projects:As per coding standards in finally block we should write resource releasing logic (or) clean up code. Resource releasing logic means unreferencing objects those are created in try block.
For Example in real time projects we create JDBC objects in try block and at the end of the try we must close those objects. Since the statement written in try and catch are not guaranteed to be executed we must place them in finally block.
Program for try, catch with finallyclass Example2 { public static void main(String[] args){ try{ System.out.println("I am in try"); } catch(ArithmeticException ae){ System.out.println("I am in catch"); } finally { System.out.println("I am in finally"); } System.out.println("After try/catch/finally"); } }