try catch finally return?
阿新 • • 發佈:2019-01-27
/** * try catch finally 中含有return 總結 * Created by nier_ni on 2014/8/6. */ public class TrySequency { /** * try catch finally 沒有異常 沒有return 正常執行 */ public static void tryNoException(){ try{ System.out.println("try塊執行"); }catch(Exception e){ System.out.println("catch塊執行"); }finally{ System.out.println("finally塊執行"); } } /** * try 中異常 catch finally */ public static void tryException(){ int i = 0; try { System.out.println("try塊執行"); int j = 5/i; }catch (Exception e){ System.out.println("catch塊執行"); }finally { System.out.println("finally塊執行"); } } /** * try 中有return catch finally 沒有異常 */ public static String tryReturnNoException(){ try { System.out.println("tryReturnNoException中try"); return "try 中 return"; }catch (Exception e){ System.out.println("tryReturnNoException中catch"); }finally { System.out.println("tryReturnNoException中finally"); } return "0"; } /** * try 中有return catch finally 有異常 */ public static String tryReturnException(){ int i = 0; try { System.out.println("tryReturnException中try"); int j = 5 / i; return "try 中 return"; }catch (Exception e){ System.out.println("tryReturnException中catch"); }finally { System.out.println("tryReturnException中finally"); } return "0"; } /** * try catch finally 之後有return 沒有異常 */ public static String tryNoExceptionFinallyReturn(){ try{ System.out.println("try塊執行"); }catch(Exception e){ System.out.println("catch塊執行"); }finally{ System.out.println("finally塊執行"); } return "finally之後的return"; } /** * try catch finally 之後有return 有異常 */ public static String tryExceptionFinallyReturn(){ int i = 0 ; try{ System.out.println("try塊執行"); int j = 3/0; }catch(Exception e){ System.out.println("catch塊執行"); }finally{ System.out.println("finally塊執行"); } return "finally之後的return"; } /** * try catch finally (catch finally中有return)返回那個return */ public static int tryExceptionCatchFinallyReturnNoException(){ int i = 1; try { System.out.println("try塊開始執行\t" + i ); int j = 5/i; }catch(Exception e){ System.out.println("catch塊開始執行\t" + i); i++; return i; }finally { System.out.println("finally塊開始執行\t" + i); i = 100; } return i; } /** *try catch finally(catch finally 中有return)有異常 返回那個return */ public static int tryExceptionCatchFinallyException(){ int i = 0; try { System.out.println("try塊開始執行\t" + i ); int j = 5/i; }catch(Exception e){ System.out.println("catch塊開始執行\t" + i); i++; return i; }finally { System.out.println("finally塊開始執行\t" + i); i = 100; } return i; } public static void main(String[] args) { /*System.out.println("try catch finally 沒有異常 沒有return 正常執行"); tryNoException(); System.out.println("----------try 中異常 catch finally------------"); tryException(); System.out.println("-----try 中有return catch finally 沒有異常-----"); System.out.println(tryReturnNoException()); System.out.println("-----try 中有return catch finally 有異常-----"); System.out.println(tryReturnException()); System.out.println("-----try catch finally 之後有return 沒有異常-----"); System.out.println(tryNoExceptionFinallyReturn()); */ /* System.out.println("-----try catch finally 之後有return 有異常-----"); System.out.println(tryExceptionFinallyReturn()); System.out.println("-------try catch finally (catch finally中有return)--------沒有異常"); System.out.println(tryExceptionCatchFinallyReturnNoException());*/ System.out.println("------try catch finally(catch finally中有return)------有異常"); System.out.println(tryExceptionCatchFinallyException()); } }