public static void main(String[] args) { // // TODO Auto-generated method stub try { System.out.println("how's the weather today?"); } catch (Exception e) { System.out.println("i don't know"); }finally{ System.out.println("weather is fine"); }
Output:java
how's the weather today?
weather is fine測試
這個你們都懂吧,我就不說什麼了。code
若是我加return的話,結果會怎麼樣?上測試代碼:it
public static void main(String[] args) { // // TODO Auto-generated method stub try { System.out.println("how's the weather today?"); return; } catch (Exception e) { System.out.println("i don't know"); return; }finally{ System.out.println("weather is fine"); }
結果仍是:io
how's the weather today?
weather is fineclass
代碼走完,可能有些人得出結論:finally必定會執行。static
不要着急,尚未完,再跑一段代碼:co
public static void main(String[] args) { // // TODO Auto-generated method stub try { System.out.println("how's the weather today?"); System.exit(0); } catch (Exception e) { System.out.println("i don't know"); }finally{ System.out.println("weather is fine"); }
Output:return
how's the weather today?void
觀看輸出發現finally塊並無被執行,JVM都退出了,還怎麼運行呢。因此之後有人問的話,能夠這樣說,在JVM正常運行的狀況下,finally塊必定會執行。