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
  • 19
  • 38

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
  • 513,304
  • 74
  • 731
  • 1,106
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,147
  • 6
  • 26
  • 42