1. 程式人生 > >異常(2)----異常處理(try...cache...finally、throws)

異常(2)----異常處理(try...cache...finally、throws)

二、try...catch異常處理

當執行程式時,可能丟擲異常。可以對可能出現異常的程式捕獲異常並處理。【例1】

當呼叫帶有throws的方法(指可能丟擲非RuntimeException的方法),必須對異常進行捕獲處理或者丟擲。【例2】

如果不對帶有throws的方法(指可能丟擲非RuntimeException的方法)進行異常處理或丟擲,則會報錯。【例3】

try...catch...finally

格式:

        try{

              //被檢測的程式碼。有可能會產生異常的程式碼。

        }catch(異常類名 變數){

              //異常的處理方式

              //這裡面可以任意寫,比如寫迴圈、遍歷、呼叫方法、變數運算等都是可以的。只要有catch就叫處理了異常。

        }finally{

              //必須要執行程式碼

        }

例1:對可能出現異常的程式碼進行異常捕獲,並處理。

package cn.itcast.demo04;

public class Test {     public static void main(String[] args) {         int[] arr = {1,22,3};         try{             int i = arr[3];             System.out.println(i);         }catch(Exception e){             e.printStackTrace();             System.out.println("aaaa");         }         System.out.println("bbbb");                                    //對異常進行處理之後,後續程式碼還能繼續執行。              }      }

執行結果:

java.lang.ArrayIndexOutOfBoundsException: 3     at cn.itcast.demo04.Test.main(Test.java:7) aaaa bbbb

例2:當呼叫帶有throws關鍵字的方法時,必須要對異常進行捕獲或者再丟擲去。

package cn.itcast.demo03;

public class Test {     public static void main(String[] args) {         int[] arr = null;         int i;         try{             i = fun(arr);             System.out.println(i);         }catch(Exception e){             e.printStackTrace();             System.out.println("aaaaa");         }         System.out.println("bbbbb");     }          public static int fun(int[] arr) throws Exception{         if(arr == null){             throw new Exception("陣列不存在");         }         return arr[3];     } }

執行結果:

java.lang.Exception: 陣列不存在     at cn.itcast.demo03.Test.fun(Test.java:19)     at cn.itcast.demo03.Test.main(Test.java:8) aaaaa bbbbb  

例3:如果不對帶有throws關鍵字的方法進行異常處理或丟擲,則會報錯。

package cn.itcast.demo04;

public class Test {     public static void main(String[] args) {         int[] arr = null;         int i = fun(arr);         System.out.println(i);                 }          public static int fun(int[] arr) throws Exception{         if(arr == null){             throw new Exception("沒有該陣列");         }         return arr[2];     }      }

執行結果:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:      Unhandled exception type Exception

    at cn.itcast.demo04.Test.main(Test.java:6)

三、throws繼續丟擲異常

當呼叫了帶有throws的方法(指可能丟擲非RuntimeException的方法)時,如果不知道如何進行處理,可以繼續丟擲。【例1】

例1:

package cn.itcast.demo02;

public class Test {     public static void main(String[] args) throws Exception{      //因為main方法呼叫了帶有throws關鍵字的方法fun(),而且丟擲的不是                                                                                                     RuntimeException,所以必須對fun()可能丟擲的異常進行處理。而main                                                                                                   也不知道怎麼處理,所以繼續丟擲。

                                                                                               //如這裡不做其他處理,也不寫throws Exception,會報錯。         int[] arr = {};         int i = fun(arr);         System.out.println(i);     }          //fun():返回最後一個元素*2     public static int fun(int[] arr) throws Exception{                 //標明這個方法可能會丟擲Exception異常。         if(arr == null){             throw new Exception("傳入的陣列不存在");         }         if(arr.length == 0){             throw new Exception("傳入的陣列沒有元素");         }

                 int i = arr[arr.length -1];         return i*2;     } }

執行結果:

Exception in thread "main" java.lang.Exception: 傳入的陣列沒有元素     at cn.itcast.demo02.Test.fun(Test.java:16)     at cn.itcast.demo02.Test.main(Test.java:6)