Behaviour of return statement in catch and finally
finally
is always executed (the only exception is System.exit()
). You can think of this behavior this way:
- An exception is thrown
- Exception is caught and return value is set to 5
- Finally block gets executed and return value is set to 10
- The function returns
It is overridden by the one in finally
, because finally
is executed after everything else.
That's why, a rule of thumb - never return from finally
. Eclipse, for example, shows a warnings for that snippet: "finally block does not complete normally"