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,
finallywill be called after the execution of thetryorcatchcode blocks.The only times
finallywon’t be called are:System.exit()Runtime.getRuntime().halt(exitStatus)tryorcatchblockkill -9 <pid>on UNIXfinallyblock is going to be executed by a daemon thread and all other non-daemon threads exit beforefinallyis called