What is finally block?

What is finally block?

  The finally block in java, if used, is placed after a try block and the catch blocks that follow it. The finally block contains code that will be run whether or not an exception is thrown in a try block.

 As its name shows wheter the exception occur or not the finally code will surely excute. The general syntax looks like this:

public void Method{
   Try {
   // code
   }

   Catch(Exception x) {
   //  code
   }

   Catch(ExceptionClass y) {
   //  code
   }

   finally{
   //this code will be executed whether an exception occurs or not
   //is thrown or caught
   }
}

Read More →