異常(實際使用直接try-catch)
1.常見系統異常
異常 |
異常的解釋 |
ClassNotFoundException |
未找到要裝載的類 |
ArrayIndexOutOfBoundsException |
數組訪問越界 |
FileNotFoundException |
文件找不到 |
IOException |
輸入、輸出錯誤 |
NullPointerException |
空指針訪問 |
ArithmeticException |
算術運算錯誤,如除數為0 |
NumberFormatException |
數字格式錯誤 |
InterruptedException |
中斷異常。 |
2.自定義異常
(1)定義異常類—繼承Exception類
(2)在方法內拋出異常
throw new 異常類();
(3)聲明方法存在異常
在方法頭的尾部加上:throws 異常類列表
3.實戰
1.假設學校開設舞蹈課程,輸入男生、女生的數量,如果男生或女生的數量為0,則舞蹈課程無法開設,如果不為0,則根據男生/女生人數的多少計算男生/女生的partner數量。請用異常類處理數量為0的情況,重新編寫下面的代碼。
要求運行結果如下:




主函數
public class DancerLesson { public static void main(String[] args) { Scanner input=new Scanner(System.in); try{ System.out.println("Enter number of male dancers:"); int men=input.nextInt(); System.out.println("Enter number of female dancers:"); int women=input.nextInt(); if(men==0||women==0) throw new myExcpetion(men,women); else { if(men>=women) System.out.println("Each woman must dance with"+men/(double)women+"men."); else System.out.println("Each man must dance with"+women/(double)men+"women."); System.out.println("Begin the lesson"); } }catch(myExcpetion e) { System.out.println(e); } } }
異常類
public class myExcpetion extends Exception { public String tostring() { return "人數錯誤"; } public myExcpetion(int men,int women) { System.out.print("Lesson is canceled."); if(men==0&&women==0) System.out.println("No student"); else if(men>0&&women==0) System.out.println("No men"); else System.out.println("No women");
結果
2.要求聲明定義兩個Exception的異常子類:NoLowerLetter類和NoDigit類
再聲明一個People類,該類中的void printLetter(char c)方法拋出NoLowerLetter異常類對象,void
printDigit(char c)方法拋出NoDigit異常類對象。請補充下列代碼中【代碼1】到【代碼6】
//ExceptionExample.java public class NoLowerLetter // 類聲明,聲明一個Exception的子類NoLowerLetter { public void print() { System.out.printf("%c",'#'); } } public class NoDigit extends Exception // 類聲明,聲明一個Exception的子類NoDigit { public void print() { System.out.printf("%c",'*'); } } class People { void printLetter(char c) throws NoLowerLetter { if(c<'a'||c>'z') { NoLowerLetter noLowerLetter= new NoLowerLetter(); // 創建NoLowerLetter類型對象 throw noLowerLetter; // 拋出noLowerLetter } else { System.out.print(c); } } void printDigit(char c) throws NoDigit { if(c<'1'||c>'9') { NoDigit noDigit= new NoDigit(); // 創建NoDigit()類型對象 throw noDigit; // 拋出noDigit } else { System.out.print(c); } } } public class ExceptionExample { public static void main (String args[ ]){ People people=new People( ); for(int i=0;i<128;i++) { try { people.printLetter((char)i); } catch(NoLowerLetter e) { e.print(); } } for(int i=0;i<128;i++) { try { people.printDigit((char)i); } catch(NoDigit e) { e.print( ); } } } }
3.選擇題
1)設有如下代碼:
try { tryThis(); return; } catch (IOException x1) { System.out.println("exception 1"); return; } catch (Exception x2) { System.out.println("exception 2"); return; } finally { System.out.println("finally"); }
如果tryThis() 拋出 NumberFormatException,則輸出結果是? (C)
A. 無輸出
B. "exception 1", 后跟 "finally"
C. "exception 2", 后跟 "finally"
D. "exception 1"
E. "exception 2"
2)設有如下方法:
public void test() { try { oneMethod(); System.out.println("condition 1"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("condition 2"); } catch(Exception e) { System.out.println("condition 3"); } finally { System.out.println("finally"); } }
如果oneMethod正常運行,則輸出結果中有哪些?(AD)
A. condition 1
B. condition 2
C. condition 3
D. finally
3) 設有如下代碼:
public void fun () { int i; try { i=System.in.read (); System.out.println("Location 1"); } catch (IOException e) { System.out.println("Location 2"); } finally { System.out.println("Location 3"); } System.out.println("Location 4"); }
如果有一個IOException發生, 則輸出有哪些? (BCD)
A. Location 1
B. Location 2
C. Location 3
D. Location 4
4) 設有如下代碼:
1 String s = null
2 if ( s != null & s.length() > 0) 3 System.out.println("s != null & s.length() > 0"); 4 if ( s != null && s.length() > 0) 5 System.out.println("s != null & s.length() > 0"); 6 if ( s != null || s.length() > 0) 7 System.out.println("s != null & s.length() > 0"); 8 if ( s != null | s.length() > 0) 9 System.out.println("s != null | s.length() > 0");
以下行中哪些會產生空指針異常。 (D)
A. 2,4
B. 6,8
C. 2,4,6,8
D. 2,6,8
5) 類Test1、Test2定義如下:
1.public class Test1 { 2. public float aMethod(float a,float b) throws IOException { 3. } 4. } 5. public class Test2 extends Test1{ 6. 7.
將以下哪種方法插入行6是不合法的。 (A)
A、float aMethod(float a,float b){ }
B、public int aMethod(int a,int b)throws Exception{ }
C、public float aMethod(float p,float q){ }
D、public int aMethod(int a,int b)throws IOException{ }