
Originally Posted by
rahulvegi
finally block
- it handles uncatched exceptions.
- this must be followed with the try and catch block.
- if try block throw an exception then no other catch block handle that exception. now finally block executes that exception.
example:
try {
//it throws a zero by division error
}
catch(NullPointerException)
{
// it throws NullPointerException instead of Zero by division error
}
finally{
printStackTrace();
}
- in the above example no catch statement to handle the exception i.e throw by the try then finally block can handle that exception by using the printstacktrace() method. printstacktrace(), which handles any other exption.
main advantage is to be finally block must be executed.
note:dont enter any statement between try,catch and finally block even System.out.print() also. if u use compiler gives an error