Exception handling is an important concept in java to help developers detecting error conditions in code and how to recover from it .There are three kinds of exceptions in java
Checked Exceptions : If you think the end user of the api can recover from an error condition like invalid input , checked exceptions are used . End user of your api will have to handle the checked exceptions by throwing it again or put it in try catch block .
UnChecked Exceptions : Unchecked Exceptions are fault of developers , or java programming mistakes .It has nothing to do with the end user of api . So in unchecked exceptions , neither we need to surround it with try catch nor we need to declare throws clause . All SubClasses of java.lang.RunTimeException
are unchecked exception . for example NullPointerException is a RunTimeException because if the developer checked for null conditions , this exception would not have thrown . So a simple rule is RuntimeExceptions are developer's fault .
Error : Error indicates resource exhaustion and internal errors of jvm , where the program can not procees simply . like the memory is filled up and there is not enough memory available to execute the program then java.lang.outofmemoryerror is thrown and execution is halted abruptly .
java.lang.Throwable is the super class of all the errors and exceptions
Catching The Exception Using Try And Catch
a try catch block is used to handle a exception which might happen during the flow of program . when a exception is thrown the control goes to catch block and then you can take appropriate action.
try {
// code which could throw exception
}
catch(Exception e1) {
//Catch block
//Handle exception like notify user or halt program
}
Multiple catch Blocks:
A try block can have multiple catch block in order to catch different kind of exceptions , but you can not declare a broader exception first then a specific exception .
try {
// code
}
catch(Exception e1) {
//do something
}
catch(ExceptionType2 e2)
{
//do something else
}
catch(ExceptionType3 e3)
{
//do something
}
USING FINALLY-
Finally block executes regardless of whether an exception is thrown or not , basically it is used to clean up resources which are used in code , like closing the file stream or closing the connection
try{
//
}
catch(MyFirstException)
{
//Catch block
}
catch(MySecondException)
{
//Catch block
}
finally
{
//close database connection
}
Finally block can override return statement also , consider this simple code
public class Test { public static void main(String[] args) { System.out.println(new Test().test()); } public int test() { try { return 5; //throw new RuntimeException(); } catch(Exception e ) { return 3; } finally{ return 2; } } }this code prints 2 instead of 5 .
but if you change the code slightly
public class Test { public static void main(String[] args) { System.out.println(new Test().test()); } public int test() { int i =0; try { return i; //throw new RuntimeException(); } catch(Exception e ) { return 3; } finally{ i=2; } } }the answer will be 0 , because here the return statement computes the value to return as 0 and then goes to finally block and return value
Feel free to post comments if you have any questions