What is finally block in Java?

Java finally block is always executed whether exception is handled or not. It identifies a block of statements that needs to be executed regardless of whether or not an exception occurs within the try block. It is not mandatory to include a finally block at all, but if you do, it will run regardless of whether an exception was thrown and handled by the try and catch parts of the block.

The finally will always execute unless

  1. System.exit() is called.
  2. The JVM crashes.
  3. The try{} block never ends (e.g. endless loop).

Exceptions in Java

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. It provide a way to transfer control from one part of a program to another. More about.... Java Exceptions