6

Possible Duplicate:
In Java, does return trump finally?

What does this function return?

public int wasExceptionThrown() {
   try {
     if(1==1)
        throw new RuntimeException();
     return 1;
   } catch(Exception e) {
     return 2;
   } finally {
     return 3;
   }
   return 0;
}
Community
  • 1
  • 1
Paul Nikonowicz
  • 3,883
  • 21
  • 39

2 Answers2

7

If you call System.exit(0); then finally blocks are not called as the thread is shutdown immediately. In all other cases finally is called when the block exits (assuming it does)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

Finally called before return.

The only time finally won't be called is if you call System.exit() or if the JVM crashes first.

lichengwu
  • 4,277
  • 6
  • 29
  • 42