Java語言程式設計 上機實驗5 異常

來源:互聯網
上載者:User

標籤:enter   try   屬性   建立   分享   方法   java語言   div   ext   

Java語言程式設計 上機實驗5

實驗目的

理解異常的概念,掌握Java的異常處理機制、常見異常的捕獲方法、自訂異常及其拋出和捕獲方法。

實驗內容:

  1. 常見異常的捕獲:

編寫一個程式,分別產生ArrayIndexOutOfBoundsException類型和NumberFormatException類型的異常;然後改寫程式,對這兩種類型的異常進行捕獲併合理地處理。

另外,思考並回答問題:能否0同時拋出ArrayIndexOutOfBoundsException類型和NumberFormatException類型的異常?為什嗎?

  1. 自訂異常的定義、拋出和捕獲:

(1) 自訂兩個異常類:非法姓名異常IllegaNameException和非法地址異常IllegalAddressException。

(2) 定義Student類包含Name和Address屬性,和setName、setAddress方法,當姓名長度小於1或者大於5時拋出IllegaNameException,當地址中不含有“省”或者“市”關鍵字時拋出IllegalAddressException。

(3) 編寫程式拋出這兩種異常,在main方法中進行捕獲併合理地處理。

實驗要求:

1. 在實驗報告中給出程式運行結果。

2. 來源程式代碼附到實驗報告的最後。

3. 認真填寫實驗報告並妥善存檔,在上機實驗課之前發送電子版實驗報告至 [email protected]。

注意,“郵件標題”與“附件實驗報告檔案名稱”均為:

Java1實驗報告N_學號_姓名

其中N為阿拉伯數字,指第幾次實驗,例如:

Java1實驗報告3_ 2012010400_李雷

請嚴格按照規定的格式發送郵件(注意標題中“Java”後的數字“1”),郵件標題不符合格式要求等同於未交作業,並在規定的時間發送實驗報告郵件,到期無效。

4. 實驗報告雷同者將不能得到相應的平時分。

 

 

附錄:實驗報告

實驗題目__實驗5________

學號__2014011329__    姓名___許愷____     班級___電腦14-1___     時間____________

實驗題目解答

一.

 

二.

 

:來源程式

一.

package com.Test5;public class Test5 {    public static void main(String[] args) {        // TODO Auto-generated method stub        int a[]=new int[8];        int b;        try{            for(int i=0;i<10;i++){                System.out.println(a[i]);            }        }        catch(ArrayIndexOutOfBoundsException e){            System.out.println("數組越界");        }        try{            b=Integer.parseInt("23gf");        }        catch(NumberFormatException e){            System.out.println("類型格式轉換錯誤");        }    }}二.Test55.java    package com.Test55;public class Test55 {    /**     * @param args     * @throws IllegalAddressException      * @throws IllegaNameException      */    public static void main(String[] args) throws IllegalAddressException, IllegaNameException {        // TODO Auto-generated method stub        Student s=new Student() ;        s.setName("測試姓名字串");        s.setAddress("測試地址字串");    }}Student.javapackage com.Test55;public class Student {    String Name;    String Address;    public void setName(String Name)throws IllegaNameException{        if(Name.length()<1||Name.length()>5)            throw new IllegaNameException(Name+",長度出錯,應在1-5之間。");        this.Name=Name;    }    public void setAddress(String Address)throws IllegalAddressException{        boolean j=false;        for(int i=0;i<Address.length();i++){            if(Address.charAt(i)==‘省‘||Address.charAt(i)==‘市‘){                j=true;                break;            }                        }        if(j==false){            throw new IllegalAddressException(Address+"中沒有‘省’或‘市’出現,請修正");        }        this.Address=Address;    }}IllegaNameException.javapackage com.Test55;public class IllegaNameException extends Exception {    public  IllegaNameException(){        super();    }    public  IllegaNameException(String s){        super(s);    }}IllegalAddressException.javapackage com.Test55;public class IllegalAddressException extends Exception {    public IllegalAddressException(){            super();    }    public IllegalAddressException(String s){            super(s);    }}

心得和體會:

對於異常的處理經過編程之後才明白他的含義和用法,遵循java中的規則,只是提供一個建立異常的平台,定義也是在外面,之前理解錯誤了,以為建立異常都是在一個類中,對我很有協助。

Java語言程式設計 上機實驗5 異常

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.