Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What comes first - finally or catch block?

Tags:

Consider the following test case:

public class Main {      static int a = 0;      public static void main(String[] args) {         try {             test();             System.out.println("---");             test2();         }         catch(Exception e) {             System.out.println(a + ": outer catch");             a++;         }     }      public static void test()     {         try {             throw new Exception();         }         catch (Exception e) {             System.out.println(a + ": inner catch");             a++;         }         finally {             System.out.println(a + ": finally");             a++;         }     }      public static void test2() throws Exception     {         try {             throw new Exception();         }         finally {             System.out.println(a + ": finally");             a++;         }     } } 

With output:

0: inner catch 1: finally --- 2: finally 3: outer catch 

What's the explanation for why in test() catch happens before finally while in test2() it's the other way around?

like image 898
Yuval Adam Avatar asked Jun 24 '10 11:06

Yuval Adam


People also ask

Can finally come before catch block?

Yes, finally will be called after the execution of the try or catch code blocks.

Can you catch block after finally?

So yes, finally is performed last, but only for the try block it's attached to.

What can come before finally block?

In normal case when there is no exception in try block then the finally block is executed after try block. However if an exception occurs then the catch block is executed before finally block.

Will finally run after catch?

The finally block on a try / catch / finally will always run — even if you bail early with an exception or a return . This is what makes it so useful; it's the perfect place to put code that needs to run regardless of what happens, like cleanup code for error-prone IO.


1 Answers

The key points are these:

  • In a try-(catch)-finally block, the finally for that particular try block is performed last
  • You can nest try blocks within another, and each of those nested try blocks can have their own finally, which would be performed last for those individual try blocks

So yes, finally is performed last, but only for the try block it's attached to.

So given the following snippet:

try {     try {         throw null;     } finally {         System.out.println("Finally (inner)");     } } catch (Throwable e) {     System.out.println("Catch (outer)"); } 

This prints (as seen on ideone.com):

Finally (inner) Catch (outer) 

Observe that:

  • Within (inner), Finally is last (whether or not any catch was successful)
  • Catch (outer) follows Finally (inner), but that's because Finally (inner) is nested inside another try block within (outer)

Similarly, the following snippet:

    try {         try {             throw null;         } catch (Throwable e) {             System.out.println("Catch (inner)");         } finally {             System.out.println("Finally (inner)");             throw null;         }     } catch (Throwable e) {         System.out.println("Catch (outer)");     } 

This prints (as seen on ideone.com):

Catch (inner) Finally (inner) Catch (outer) 

References

  • JLS 14.20.2 Execution of try-catch-finally

Related questions

  • In Java, does return trump finally?
  • In Java, is the “finally” block guaranteed to be called (in the main method)?
  • Does a finally block always run?
  • what happens to finally block in the following cases?
like image 128
polygenelubricants Avatar answered Sep 30 '22 18:09

polygenelubricants