Finally block
We know that whenever exception occurs, we need to handle such exception using try/catch
But, If we miss to handle such exception then program terminates abruptly.
This skips some of the important statements to be executed in the program.
So, in such scenario, finally blocks comes to rescue us.
All the statements or a piece of code we write in finally block executes always no matter whether exception occurs or not.
So, we must keep any important piece of code to be executed inside finally block.
Example: Closing resources like database connection, stream.
Syntax of finally block
Case 1: with catch block
- try {
- //Piece of code that may cause an exception
- }
- catch {
- //Handling exception
- }
- finally {
- //Statements to be executed always
- }
try { //Piece of code that may cause an exception } catch { //Handling exception } finally { //Statements to be executed always }
Case 2: without catch block
- try {
- //Piece of code that may cause an exception
- }
- finally {
- //Statements to be executed always
- }
try { //Piece of code that may cause an exception } finally { //Statements to be executed always }
Example :
Case 1 : Exception does not occur
- class FinallyBlockExample1{
- public static void main(String args[]){
- try{
- int result=10/5;
- System.out.println(result);
- }
- catch(ArithmeticException e){
- System.out.println(e);
- }
- finally{
- System.out.println("finally block is always executed");
- }
- System.out.println("continuing further flow...");
- }
- }
class FinallyBlockExample1{ public static void main(String args[]){ try{ int result=10/5; System.out.println(result); } catch(ArithmeticException e){ System.out.println(e); } finally{ System.out.println("finally block is always executed"); } System.out.println("continuing further flow..."); } }
Case 2 : Exception occurs but not handled
- class FinallyBlockExample2{
- public static void main(String args[]){
- try{
- int result=10/0;
- System.out.println(result);
- }
- finally{
- System.out.println("finally block is always executed");
- }
- System.out.println("continuing further flow...");
- }
- }
class FinallyBlockExample2{ public static void main(String args[]){ try{ int result=10/0; System.out.println(result); } finally{ System.out.println("finally block is always executed"); } System.out.println("continuing further flow..."); } }
Case 3 : Exception occurs and its handled
- class FinallyBlockExample3{
- public static void main(String args[]){
- try{
- int result=10/0;
- System.out.println(result);
- }
- catch(ArithmeticException e){
- System.out.println(e);
- }
- finally{
- System.out.println("finally block is always executed");
- }
- System.out.println("continuing further flow...");
- }
- }
class FinallyBlockExample3{ public static void main(String args[]){ try{ int result=10/0; System.out.println(result); } catch(ArithmeticException e){ System.out.println(e); } finally{ System.out.println("finally block is always executed"); } System.out.println("continuing further flow..."); } }
We can see that, in all the above scenarios finally block is executed always no matter whether exception has occurred or not.
So, we should always prefer using finally block to execute important code irrespective of whether exception occurs or not.
When finally, block does not gets executed ?
1) Whenever current thread terminates
2) When we use system.exit() method
3) Exception occurred in finally block and not handled then finally block does not execute further.
Finally block with system.exit()
System.exit() statement behaves differently than return statement. Unlike return statement whenever System.exit() gets called in try block then finally block doesn’t execute at all.
Example :
- try {
- System.out.println("Inside try block");
- System.exit(0);
- }
- catch (Exception e) {
- System.out.println(e);
- }
- finally {
- System.out.println("finally block");
- }
try { System.out.println("Inside try block"); System.exit(0); } catch (Exception e) { System.out.println(e); } finally { System.out.println("finally block"); }
In the above program, finally block does not get executed as there is system.exit(0) in the try block.
Key Points on finally
Finally block should be associated with try block, It means we can not use finally block without try block. Finally block is not mandatory but good to have to execute important piece of code Finally block executes after try block if no exception and it executes after catch block if there is any exception and its handled. Finally block executes even if there is a return statement in try block Exception might be thrown again within finally block and it is same as any other exception behavior