A finally block in Java is used along with try-catch. The code we write inside the finally block will be executed regardless of whether the exception occurred or not. Here we put some important statements, such as closing a connection, stream, etc.
Important points to remember:
- It must be associated with a try block. We cannot use finally without a try block,
- Finally block is optional,
- In cases where an exception occurs, and we have the appropriate catch block, then the catch block will be executed first, and only then finally block.
Syntax of try-catch-finally block
try { //Statements that may cause an exception } catch { //Handling exception } finally { //Statements to be executed }
Examples of using a finally block
Example 1:
class Test { public static void main(String[] args) { try { System.out.println(20 / 0); // this will throw ArithmeticException } catch (ArithmeticException ae) { System.out.println("ArithmeticException occurred!"); } finally { System.out.println("Finally block..."); } } }
Output: ArithmeticException occurred! Finally block…
Here, the catch block first gets executed, then the finally block.
Example 2:
Without catch block.
class Test { public static void main(String[] args) { try { System.out.println(20 / 0); // this will throw ArithmeticException } finally { System.out.println("Finally block..."); } } }
Output: Finally block… Exception in thread “main” java.lang.ArithmeticException: / by zero at com.company.Test.main(User.java:9)
As you can see in the example above, we did not use a catch block, so the exception was not handled.
The finally block was executed, and immediately after that the program stopped, and the error was thrown.
The finally block was executed, and immediately after that the program stopped, and the error was thrown.
Example 3:
The exception does not occur.
class Test { private String str = "Java Programming"; public static void main(String[] args) { try { Test test = new Test(); System.out.println(test.str); } catch (Exception e) { System.out.println("Exception occurred!"); } finally { System.out.println("Finally block..."); } } }
Output: Java Programming Finally block…
Here you see, although the exception did not occur, the finally block was executed.
Example 4:
Using the return keyword inside try block.
class Test { private String str = "Java Programming"; public static void main(String[] args) { Test test = new Test(); System.out.println(test.getString()); } private String getString() { try { return str; } finally { System.out.println("Finally block ..."); } } }
Output: Finally block … Java Programming
There are some cases when the finally block doesn’t execute:
- Using the System. exit() method.
- The death of a Thread
Example 5:
Using finally block with System.exit().
class Test { public static void main(String[] args) { try { System.out.println("Inside try block ..."); System.exit(0); } finally { System.out.println("Finally block ..."); } } }
Output: Inside try block …
In the example above, System.exit() caused the program to stop, so the finally block was not executed.
That’s it!