摘要:韋東山android視頻學習筆記 java
java的異常處理的原則以下:android
一、咱們先寫一個沒有對異常處理的程序,在進行除法運算的時候,除數是非零的話,運行時沒有問題的,可是除數爲零的時候,運行就會有問題,程序也不能往下執行(只打印了Begin of div)git
1 public class Div{ 2 3 public static void main(String args[]){ 4 int m = Integer.parseInt(args[0]); 5 int n = Integer.parseInt(args[1]); 6 7 System.out.println("Begin of div"); 8 int r = div(m,n); 9 System.out.println("end of div"); 10 11 System.out.println(m+"/"+n+"="+r); 12 } 13 14 public static int div(int m,int n){ 15 int r = m / n; 16 return r; 17 } 18 }
編譯運行:github
二、咱們先寫一個有對異常進行處理程序(本身處理異常),根據下面的運行結果,程序能夠捕獲到異常而且能夠正常的執行.學習
1 public class Div2{ 2 3 public static void main(String args[]){ 4 int m = Integer.parseInt(args[0]); 5 int n = Integer.parseInt(args[1]); 6 7 System.out.println("Begin of div"); 8 int r = div(m,n); 9 System.out.println("end of div"); 10 11 System.out.println(m+"/"+n+"="+r); 12 } 13 14 public static int div(int m,int n){ 15 int r = 0; 16 try { 17 r = m / n ; 18 }catch (ArithmeticException e){ 19 System.out.println(e); 20 }finally{ 21 System.out.println("This is finally of div"); 22 } 23 24 return r; 25 } 26 }
編譯運行:優化
三、咱們寫一個程序將異常拋出的類,這個拋出的異常是由main進行處理.spa
1 public class Div4{ 2 3 public static void main(String args[]){ 4 int m = Integer.parseInt(args[0]); 5 int n = Integer.parseInt(args[1]); 6 int r = 0; 7 8 System.out.println("Begin of div"); 9 try { 10 r = div(m,n); 11 }catch (ArithmeticException e){ 12 System.out.println(e); 13 } 14 System.out.println("end of div"); 15 16 System.out.println(m+"/"+n+"="+r); 17 } 18 19 public static int div(int m,int n) throws ArithmeticException{ 20 int r = 0; 21 22 r = m / n ; 23 24 return r; 25 } 26 }
編譯運行:3d
四、若是在類的方法中若是處理了異常,那樣在main方法中就不會對異常進行處理.code
1 public class Div5{ 2 3 public static void main(String args[]){ 4 int m = Integer.parseInt(args[0]); 5 int n = Integer.parseInt(args[1]); 6 int r = 0; 7 8 System.out.println("Begin of div"); 9 try { 10 r = div(m,n); 11 }catch (ArithmeticException e){ 12 System.out.println(e); 13 } 14 System.out.println("end of div"); 15 16 System.out.println(m+"/"+n+"="+r); 17 } 18 19 public static int div(int m,int n) throws ArithmeticException{ 20 int r = 0; 21 22 try{ 23 r = m / n ; 24 }catch(ArithmeticException e){ 25 System.out.println("div :"+e); 26 } 27 28 return r; 29 } 30 }
編譯運行:orm
五、若是在類的方法中若是處理了異常,同時在類方法中把異常拋出,那樣main方法也能夠捕獲到異常.
1 public class Div6{ 2 3 public static void main(String args[]){ 4 int m = Integer.parseInt(args[0]); 5 int n = Integer.parseInt(args[1]); 6 int r = 0; 7 8 System.out.println("Begin of div"); 9 try { 10 r = div(m,n); 11 }catch (ArithmeticException e){ 12 System.out.println(e); 13 } 14 System.out.println("end of div"); 15 16 System.out.println(m+"/"+n+"="+r); 17 } 18 19 public static int div(int m,int n) throws ArithmeticException{ 20 int r = 0; 21 22 try{ 23 r = m / n ; 24 }catch(ArithmeticException e){ 25 System.out.println("div :"+e); 26 throw e; 27 } 28 29 return r; 30 } 31 }
編譯運行:
六、如今咱們上面第5個例子的代碼,只有對這種算術運行的異常進行處理,若是我傳入的參數個數不對,還有參數的格式也不對,程序是處理不了的。
爲了修復上述的問題,咱們添加對傳入參數格式不對,還有傳入參數個數不對這兩種異常的處理。
1 public class Div7{ 2 3 public static void main(String args[]){ 4 int m = 0; 5 int n = 0; 6 int r = 0; 7 8 System.out.println("Begin of div"); 9 try { 10 m = Integer.parseInt(args[0]); 11 n = Integer.parseInt(args[1]); 12 r = div(m,n); 13 }catch (ArithmeticException e){ 14 System.out.println("main :" + e); 15 }catch (NumberFormatException e){ 16 System.out.println("main :" + e); 17 }catch (ArrayIndexOutOfBoundsException e){ 18 System.out.println("main :" + e); 19 } 20 System.out.println("end of div"); 21 22 System.out.println(m+"/"+n+"="+r); 23 } 24 25 public static int div(int m,int n) throws ArithmeticException{ 26 int r = 0; 27 28 try{ 29 r = m / n ; 30 }catch(ArithmeticException e){ 31 System.out.println("div :"+e); 32 throw e; 33 } 34 35 return r; 36 } 37 }
編譯運行結果
七、在第6個例子繼續優化,上面的程序目前只能對算術運算、參數格式還有參數個數不對的異常進行處理,其餘的狀況是沒法處理的到的,咱們能夠添加對這些異常的父類RuntimeException來捕獲異常.
1 public class Div8{ 2 3 public static void main(String args[]){ 4 int m = 0; 5 int n = 0; 6 int r = 0; 7 8 System.out.println("Begin of div"); 9 try { 10 m = Integer.parseInt(args[0]); 11 n = Integer.parseInt(args[1]); 12 r = div(m,n); 13 }catch (ArithmeticException e){ 14 System.out.println("main :" + e); 15 }catch (NumberFormatException e){ //去掉了參數個數的異常,仍然能夠捕獲到 16 System.out.println("main :" + e); 17 }catch (RuntimeException e){ 18 System.out.println("main :" + e); 19 } 20 System.out.println("end of div"); 21 22 System.out.println(m+"/"+n+"="+r); 23 } 24 25 public static int div(int m,int n) throws ArithmeticException{ 26 int r = 0; 27 28 try{ 29 r = m / n ; 30 }catch(ArithmeticException e){ 31 System.out.println("div :"+e); 32 throw e; 33 } 34 35 return r; 36 } 37 }
編譯運行結果
八、對於「不可查異常」, 系統也會拋出它,寫不寫throws效果同樣
1 public class Div9{ 2 3 public static void main(String args[]){ 4 int m = 0; 5 int n = 0; 6 int r = 0; 7 8 System.out.println("Begin of div"); 9 try { 10 m = Integer.parseInt(args[0]); 11 n = Integer.parseInt(args[1]); 12 r = div(m,n); 13 }catch (ArithmeticException e){ 14 System.out.println("main :" + e); 15 }catch (NumberFormatException e){ 16 System.out.println("main :" + e); 17 }catch (RuntimeException e){ 18 System.out.println("main :" + e); 19 } 20 System.out.println("end of div"); 21 22 System.out.println(m+"/"+n+"="+r); 23 } 24 25 //public static int div(int m,int n) throws ArithmeticException{ 26 public static int div(int m,int n){ 27 int r = 0; 28 29 try{ 30 r = m / n ; 31 }catch(ArithmeticException e){ 32 System.out.println("div :"+e); 33 throw e; 34 }finally{ 35 System.out.println("finally of div"); 36 } 37 38 return r; 39 } 40 }
編譯運行:
相關代碼存放在github,能夠下載https://github.com/zzb2760715357/100ask