Java日誌第35天 2020.8.9
阿新 • • 發佈:2020-08-09
異常的注意事項
1.多個異常分別處理
import java.util.ArrayList; import java.util.List; public class Demo02Exception { //多個異常分別處理 public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3);int[] arr = {1, 2, 3}; try{ System.out.println(arr[3]); }catch (ArrayIndexOutOfBoundsException r){ System.out.println(r); } try{ System.out.println(list.get(3)); }catch(IndexOutOfBoundsException e){ System.out.println(e); } }
2.多個異常一次捕獲,多次處理
import java.util.ArrayList; import java.util.List; public class Demo02Exception { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); int[] arr = {1, 2, 3};try{ System.out.println(arr[3]); System.out.println(list.get(3)); }catch (ArrayIndexOutOfBoundsException r){ System.out.println(r); } catch(IndexOutOfBoundsException e){ System.out.println(e); } } }
注意:
catch裡邊定義的異常變數,如果有子父類關係,則子類的的異常變數必須寫在上邊
3.多個異常一次捕獲,一次處理
import java.util.ArrayList; import java.util.List; public class Demo02Exception { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); int[] arr = {1, 2, 3}; try{ System.out.println(arr[3]); System.out.println(list.get(3)); }catch (ArrayIndexOutOfBoundsException r){ System.out.println(r); } } }
*執行時異常被丟擲可以不處理。既不捕獲也不宣告丟擲
*如果finally中有return語句,永遠返回finally中的結果,避免該情況
public class Demo03Exception { public static void main(String[] args) { int a = getA(); System.out.println(a); } public static int getA() { int a = 10; try{ return a; }catch (Exception e){ System.out.println(e); }finally { a = 100; return a; } } }
*如果父類丟擲了多個異常,子類重寫父類方法時,丟擲和父類相同的異常或者是父類異常的子類或者不丟擲異常。
*父類方法沒有丟擲異常,子類重寫父類該方法時也不可丟擲異常。此時子類產生該異常,只能捕獲處理,不能宣告丟擲。
public class Fu { public void method01() throws NullPointerException, ClassCastException{ } public void method02() throws IndexOutOfBoundsException { } public void method03() throws IndexOutOfBoundsException { } public void method04() { } } class Zi extends Fu{ //子類重寫父類方法時,丟擲和父類相同的異常 public void method01() throws NullPointerException, ClassCastException {} //子類重寫父類方法時,丟擲父類異常的子類 public void method02() throws ArrayIndexOutOfBoundsException {} //子類重寫父類方法時,不丟擲異常 public void method03() {} /* 父類方法沒有丟擲異常,子類重寫父類該方法時也不可丟擲異常。 此時子類產生該異常,只能捕獲處理,不能宣告丟擲。 */ public void method04(){ try { throw new Exception(); } catch (Exception e) { e.printStackTrace(); } } }
自定義異常類
格式
public XXXException extends Exception | RuntimeException{
新增一個空引數的構造方法
新增一個有異常資訊的構造方法
}
注意:
1.自定義異常類一般都以Exception結尾,說明該類是一個異常類
2.自定義異常類必須繼承Exception或者RuntimeException
繼承Exception:說明該異常是編譯期異常
繼承RuntimeException:說明該異常是執行期異常
/* 通過原始碼發現,所有的異常類都會有一個帶有異常資訊的構造方法 方法內部呼叫父類異常資訊的構造方法,讓父類來處理這個異常資訊 同理,無參建構函式也可以通過父類進行處理 */ public class RegisterException extends Exception{ //無參的構造方法 public RegisterException() { super(); } //含參的構造方法 public RegisterException(String s) { super(s); } }
練習
模擬註冊操作,如果使用者名稱已存在,丟擲異常並提示
import java.util.Scanner; public class Demo01RegisterException { static String[] usernames = {"張三", "李四", "王五"}; public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("請輸入將要註冊的使用者姓名:"); String username = sc.next(); check(username); } public static void check(String username) { for (String s : usernames) { if(s.equals(username)){ try { throw new RegisterException("親,該使用者名稱已經被註冊!"); } catch (RegisterException e) { e.printStackTrace(); return;//結束 } } } System.out.println("親,恭喜您註冊成功!"); } }
結果如下:
問題:
父類方法沒有丟擲異常,子類重寫父類該方法時也不可丟擲異常。此時子類產生該異常,只能捕獲處理,不能宣告丟擲。
為什麼並不會報錯?
明日任務:學習IO流