Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit to use "finally" after try-catch block in java? [closed]

The "finally" block is always executed when the try-catch ends, either in case of exception or not. But also every line of code outside and after the try-catch is always executed. So, why should I use the finally statement?

Example:

try {     //code... } catch (Exception e) {     //code... } finally {     System.out.println("This line is always printed"); } System.out.println("Also this line is always printed !! So why to use 'finally'?? "); 
like image 350
Aarath Avatar asked Apr 02 '13 15:04

Aarath


People also ask

What is the purpose of a finally block in the try catch block?

A Finally block is useful for running any code that must execute even if there is an exception. Control is passed to the Finally block regardless of how the Try...Catch block exits. The code in a Finally block runs even if your code encounters a Return statement in a Try or Catch block.

Can we use Finally block after try block?

Syntax. The code which is prone to exceptions is placed in the try block. When an exception occurs, that exception occurred is handled by catch block associated with it. Every try block should be immediately followed either by a catch block or finally block.

What is the use of try and finally without catch?

@barth When there's no catch block the exception thrown in finally will be executed before any exception in the try block. So If there are two exceptions one in try and one in finally the only exception that will be thrown is the one in finally .

Is finally block mandatory with try catch?

Nope, not at all. Its not mandatory to put catch after try block, unless and until the try block is followed by a finally block. Just remember one thing, after try, a catch or a finally or both can work.


2 Answers

The most useful case is when you need to release some resources :

InputStream is = ... try {     //code... } catch (Exception e) {     //code... } finally {     is.close(); } 

More generally, you use it when you want to be sure your code is executed at the end, even if there was an exception during execution :

long startTime = System.currentTimeMillis(); try {     //code... } catch (Exception e) {     //code... } finally {     long endTime = System.currentTimeMillis();     System.out.println("Operation took " + (endTime-startTime) + " ms"); } 

The idea of this finally block always being executed is that it's not the case for the first line following the whole block

  • if the catch block lets some throwable pass
  • if it rethrows itself an exception, which is very frequent
like image 174
3 revs Avatar answered Sep 22 '22 09:09

3 revs


The last System.out.println (after the finally block) will only be run if the exception thrown in the try block is actuaclly caught with a catch block and if the execution is not interrupted by e.g. a return statement.

In your example, the finally block will always be run, but the execution will only continue past the finally block if no Error is thrown in the try block (it would not be caught), if no Throwable is thrown in the catch block and there is no other statement, which will interrupt execution.

like image 32
jarnbjo Avatar answered Sep 20 '22 09:09

jarnbjo