Java finally block

  • Java finally block is a block that is used to execute important code such as closing connection, stream etc.
  • Java finally block is always executed whether exception is handled or not.
  • Java finally block follows try or catch block.
  • Finally block in java can be used to put “cleanup” code such as closing a file, closing connection etc.
  • For each try block there can be zero or more catch blocks, but only one finally block.

NOTE:

  • If you don’t handle exception, before terminating the program, JVM executes finally block(if any).
  • The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).
[pastacode lang=”java” manual=”public%20static%20void%20main(String%5B%5D%20args)%20%0A%7B%0A%20%20%20%20System.out.println(Test.test())%3B%0A%7D%0A%0Apublic%20static%20int%20test()%20%0A%7B%0A%20%20%20%20try%20%0A%7B%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%7D%0A%20%20%20%20finally%20%0A%7B%0A%20%20%20%20%20%20%20%20System.out.println(%22finally%20trumps%20return.%22)%3B%0A%20%20%20%20%7D%0A%7D%0A” message=”java code” highlight=”” provider=”manual”/] [ad type=”banner”]

Output:

finally trumps return.

0

Code that shows finally runs after return

[pastacode lang=”java” manual=”class%20SomeClass%0A%7B%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%2F%2F%20call%20the%20proveIt%20method%20and%20print%20the%20return%20value%0A%20%20%20%20%09System.out.println(SomeClass.proveIt())%3B%20%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20static%20int%20proveIt()%0A%20%20%20%20%7B%0A%20%20%20%20%09try%20%0A%7B%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%09return%201%3B%20%20%0A%7D%20%20%0A%20%20%20%20%09finally%20%0A%7B%20%20%0A%20%20%20%20%09%20%20%20%20System.out.println(%22finally%20block%20is%20run%20%0A%20%20%20%20%20%20%20%20%20%20%20%20before%20method%20returns.%22)%3B%0A%7D%0A%20%20%20%20%7D%0A%7D%0A” message=”java code” highlight=”” provider=”manual”/] [ad type=”banner”]

Output:

finally block is run before method returns.

1

  • From the output, observe that the finally block is executed before control is returned to the System.out.println(SomeClass.proveIt());” statement – which is why the “1” is output after the “finally block is run before method returns.” text

Unique situations when finally will not run after return

The finally block will not be called after return in unique scenarios:

  • if System.exit() is called first,
  • if the JVM crashes.
  • if there is an infinite loop in the try block
  • if the power turns off

return statement in both the finally block and the try block

  • If we have a return statement in both the finally block and the try block, then anything that is returned in the finally block will actually override any exception or returned value that is inside the try/catch block.
  • Here is an example,
[pastacode lang=”java” manual=”public%20static%20int%20getANumber()%0A%7B%0A%20%20%20%20try%0A%7B%0A%20%20%20%20%20%20%20%20return%207%3B%0A%20%20%20%20%7D%20%0Afinally%0A%20%7B%0A%20%20%20%20%20%20%20%20return%2043%3B%0A%20%20%20%20%7D%0A%7D%09%0A” message=”java code” highlight=”” provider=”manual”/] [ad type=”banner”]
  • The code above will actually return the “43” instead of the “7”, because the return value in the finally block (“43”) will override the return value in the try block (“7”).
  • if the finally block returns a value, it will override any exception thrown in the try/catch block. Here is an example:
[pastacode lang=”java” manual=”public%20static%20int%20getANumber()%0A%7B%0A%20%20%20%20try%0A%7B%0A%20%20%20%20%20%20%20%20throw%20new%20NoSuchFieldException()%3B%0A%20%20%20%20%7D%20%0Afinally%20%0A%7B%0A%20%20%20%20%20%20%20%20return%2043%3B%0A%20%20%20%20%7D%0A%7D%0A” message=”java code” highlight=”” provider=”manual”/]
  • Running the method above will return a “43” and the exception in the try block will not be thrown.
  • So a return statement inside the finally block,should be avoided

 

Categorized in: