Wednesday 21 May 2014

What is the use of finally keyword in exception handling?

• In a program where exception is handled using try catch block after this finally keyword is used.
• Finally creates a block that will be executed even after a exception is thrown or not
• If no catch statement matches the exception even then finally block will be executed
• It is executed just before the method return




Example :

class FinallyDemo {
// Through an exception out of the method.
static void procA() {
try {
System.out.println("inside procA");
throw new RuntimeException("demo");
} finally {
System.out.println("procA's finally");
}
}

No comments:

Post a Comment