Considering this code, can I be absolutely sure that the finally
block always executes, no matter what something()
is?
try {
something();
return success;
}
catch (Exception e) {
return failure;
}
finally {
System.out.println("I don't know if this will get printed out");
}
Yes, finally
will be called after the execution of the try
or catch
code blocks.
The only times finally
won't be called are:
System.exit()
Runtime.getRuntime().halt(exitStatus)
try
or catch
blockkill -9 <pid>
on UNIXfinally
block is going to be executed by a daemon thread and all other non-daemon threads exit before finally
is called