[Java] 9.3. 異常 & 執行時期異常

執行時期異常RuntimeException,讓您可以不必強制使用try catch或throws處理錯誤。除此之外,你都必預使用try catch或throws處理錯誤。

 

image/svg+xml9.3. 異常&執⾏時期異常 - Exception&RuntimeExcetion class Main { public static void main( String [] args) { try { Person p = new Person ( "K123456789" , "Jack" , "Man" , 20 ); } catch ( PersonException e1){ System.out.println(e1.getMessage()); } } } 執⾏結果 Main.java:3: error: unreported exception PersonException; must be caught or declared to be thrown Person p = new Person("K123456789", "Jack", "Man", 20); ^ 1 error class PersonException extends Exception { // 略過⼀些程式 } class Person { // 略過⼀些程式 Person ( String pID, String pName , String pGender, int pAge) throws PersonException { // 略過⼀些程式 } } class Main { public static void main( String [] args) { try { Person p = new Person ( "K123456789" , "Jack" , " 男⽣ " , 20 ); } catch ( PersonException e1){ System.out.println(e1.getMessage()); } } } class PersonException extends RuntimeException { PersonException( String errorMsg){ super (errorMsg); } } // ⾝份證字號異常類別( IDException )、姓名異常類別( NameException )、 // 性別異常類別( GenderException )、年齡異常類別( AgeException // 皆繼承⾃⼈員異常類別 class Main { public static void main( String [] args) { Person p = new Person ( "K123456789" , "Jack" , " Man " , 20 ); } } 執⾏結果 Exception in thread "main" GenderException: 姓別以可是男⽣或女⽣。 at Person.<init>(Person.java:14) at Main.main(Main.java:3) class Person { private String id; private String name; private String gender; private int age; Person (String pID, String pName , String pGender, int pAge) throws PersonException { if (pID.trim().length() == 0 ) throw new IDException (" ⾝份證字號不可空⽩。 " ); if (pName.trim().length() == 0 ) throw new NameException (" 姓名不可空⽩。 " ); if (! (pGender.trim().equals(" 男⽣ " ) || pGender.trim().equals( " 女⽣ " ))) throw new GenderException (" 姓別以可是男⽣或女⽣。 " ); if (pAge < 0 || pAge > 150 ) throw new AgeException ( " 年齡必須介於 0~150 之間。 " ); } // 略過⼀些程式 } class PersonException extends Exception { // 略過⼀些程式 } class Person { // 略過⼀些程式 Person ( String pID, String pName , String pGender, int pAge) throws PersonException { // 略過⼀些程式 } } class Main { public static void main ( String [] args) throws PersonException { try { Person p = new Person ( "K123456789" , "Jack" , " Man " , 20 ); } catch ( PersonException e1){ System.out.println(e1.getMessage()); } } } J9_3_1 – Main.java 2. 不使⽤ try~catch 捕抓⼈員異常物件。這時編譯會發⽣錯誤,告訴 我們⼀定要使⽤ try~catch 捕抓⼈員異常物件。這是為什麼呢?還記得 我們於本章之初的轉型異常嗎!其在未使⽤ try~catch 的情況下,也能 編譯成功,為何此處不使⽤ try~catch ⼜會錯呢?! J9_3_2 – PersonExcep 9 on.java 1. ⼈員類別的建構式,有可能發⽣異常。 J9_3_2 – Person.java 1. 這是⼈員異常類別( PersonException ), 其繼承了異常類別( Exception )。 2. 這是⼈員類別( Person )的建構式,其透過 throws 關鍵字宣告會拋出⼈員異常類別的物件。 3. ⼈員類別的建構式,有可能發⽣異常。 4. 這時必須使⽤ try~catch 捕抓⼈員異常物件,因為⼈員異常 物件並非繼承⾃執⾏時期異常類別( RuntimeException )。 J9_3_2 – Main.java J9_3_3 – PersonExcep 9 on.java J9_3_3 – Main.java 1. ⼈員異常類別( PersonException )改繼承⾃ 執⾏時期異常類別( RuntimeException )。 2. 故意將姓別以英⽂ “Man” 的⽅式代入,這時就會產⽣姓別異常物件 GenderException ),但因為⼈員異常物件已經繼承⾃執⾏時期異常類別 RuntimeException ),因此不需要強制使⽤ try~catch 。因此若無使⽤ try~catch 捕抓異常,這時就會在執⾏的畫⾯上,出現異常發⽣的訊息。 J9_3_4 – Person.java 1. 這是⼈員類別( Person )。 2. 這是⼈員類別的建構式。 3. 雖然在此有可能拋出 4 種異常物件,但因為它們的⽗類別⼈員異 常類別是繼承⾃執⾏時期異常類別的,因此可以在不透過 throws 宣告可能拋出的異常類別的情況下,直接以將異常物件拋出。 J9_3_5 – PersonExcep 9 on.java J9_3_5 – Person.java 1. 這是⼈員異常類別( PersonException ),其繼承了異常 類別( Exception ),表⽰這個異常⼀定要處理。 2. 這是⼈員類別( Person )的建構式,其透過 throws 關鍵字宣告會拋出⼈員異常類別的物件。 J9_3_5 – Main.java 4. 雖然沒有使⽤ try~catch 補抓非繼承 RuntimeException 的異常 物件,但因為所在的⽅法 - main() ⽅法有使⽤ throws 丟出未處理的 異常,這也是可以的,編譯時會不出現未處理異常的錯誤。 3. 故意將姓別以英⽂ “Man” 的⽅式代入,這時就會 產⽣姓別異常物件( GenderException )。 為什麼透過 throws 宣告拋出異常的⼈員類別建構式, 就⼀定要使⽤ try~catch 呢? 1 1 非繼承 RuntimeException 必須強制捕抓⼈員異常物件 繼承 RuntimeException 選擇性的捕抓⼈員異常物件 繼承 RuntimeException 則不需透過 throws 定義異常 非繼承⾃ RuntimeException 若不使⽤ try~catch ,則必須使⽤ throws

留言