try {
  return 1; 
} catch (Exception ex) {
  //...
} finally {
  //...
  return 2;
}

类似“try里的return语句会不会使得finally块被跳过”或者“到底是return 1还是return 2”之类的问题常常会在面试时被问到。 这篇Oracle的文档给出了答案,

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break.

意思是return之类的关键字不会使得finally块的执行被跳过。

除非在执行try时JVM退出了(比如调用了System.exit());或者执行try的线程被中断或者杀死了。

If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.