武漢科技大學(xué)-Java實(shí)驗(yàn)報(bào)告--實(shí)驗(yàn)四(共15頁)_第1頁
武漢科技大學(xué)-Java實(shí)驗(yàn)報(bào)告--實(shí)驗(yàn)四(共15頁)_第2頁
武漢科技大學(xué)-Java實(shí)驗(yàn)報(bào)告--實(shí)驗(yàn)四(共15頁)_第3頁
武漢科技大學(xué)-Java實(shí)驗(yàn)報(bào)告--實(shí)驗(yàn)四(共15頁)_第4頁
武漢科技大學(xué)-Java實(shí)驗(yàn)報(bào)告--實(shí)驗(yàn)四(共15頁)_第5頁
已閱讀5頁,還剩11頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上信息科學(xué)與工程學(xué)院 Java程序設(shè)計(jì) 上機(jī)實(shí)驗(yàn)報(bào)告專業(yè)班級 姓 名 學(xué) 號 實(shí)驗(yàn)時(shí)間 指導(dǎo)教師 成 績 實(shí)驗(yàn)名稱實(shí)驗(yàn)四 異常類的定義及處理實(shí)驗(yàn)?zāi)康?) 了解異常處理方法。2) 熟悉并掌握常見異常的捕獲方法。3) 熟悉JDK中已經(jīng)定義的若干異常類的層次結(jié)構(gòu)。4) 掌握自定義異常類的創(chuàng)建方法。主 要 實(shí) 驗(yàn) 記 錄 及 個(gè) 人 小 結(jié) (包括部分實(shí)驗(yàn)源程序、調(diào)試結(jié)果及實(shí)驗(yàn)結(jié)果分析等)實(shí)驗(yàn)內(nèi)容:1、編寫程序?qū)崿F(xiàn)如下功能:生成并捕獲到NegativeArraySizeException和IndexOutOfBoundsException類型的異常,并顯示捕獲到的異常信息。然后

2、在此基礎(chǔ)上生成并捕獲到NullPointerException類型的異常,并顯示捕獲到的異常信息。實(shí)驗(yàn)步驟:步驟(1):編寫一個(gè)包含main方法的Application類TestException,然后定義一個(gè)方法void arraySize()生成并捕獲NegativeArraySizeException異常。步驟(2):添加一個(gè)方法void outofBound()生成并捕獲IndexOutOfBoundsException異常。步驟(3):添加一個(gè)方法void nullPointer()生成并捕獲IndexOutOfBoundsException異常。步驟(4):在main方法中分別調(diào)用

3、以上三個(gè)方法。步驟(5):將文件保存為TestException.java,然后編譯、調(diào)試應(yīng)用程序。步驟(6):將outofBound()方法中捕獲異常的語句注釋掉,重新編譯程序,看看會(huì)不會(huì)有什么語法錯(cuò)誤?如果沒錯(cuò)誤,執(zhí)行程序看結(jié)果有什么不同?步驟(7):將array方法重新定義為如下形式:void arraySize() throws NegativeArraySizeException然后修改arraySize方法中捕獲NegativeArraySizeException異常的語句執(zhí)行部分。主 要 實(shí) 驗(yàn) 記 錄 及 個(gè) 人 小 結(jié) (包括部分實(shí)驗(yàn)源程序、調(diào)試結(jié)果及實(shí)驗(yàn)結(jié)果分析等)程序代碼

4、:public class TestException public static void main(String args) arraySize(); outofBound(); nullPointer(); static void arraySize() throws NegativeArraySizeException try int Array = new int-1; catch(NegativeArraySizeException e) System.out.println (數(shù)組大小異常:+e); static void outofBound() try int i; int

5、Array=new int3; for(i=0;i=3;i+) Arrayi=i; catch(IndexOutOfBoundsException e) System.out.println (數(shù)據(jù)溢出:+e); static void nullPointer() try String s; int a; s=null; a=s.length(); catch(NullPointerException e) System.out.println (調(diào)用對象為空:+e); 主 要 實(shí) 驗(yàn) 記 錄 及 個(gè) 人 小 結(jié) (包括部分實(shí)驗(yàn)源程序、調(diào)試結(jié)果及實(shí)驗(yàn)結(jié)果分析等)程序截圖:1、 編寫程序?qū)崿F(xiàn)如下

6、功能:計(jì)算兩個(gè)數(shù)之和,參與求和運(yùn)算的每個(gè)數(shù)的值都必須在10-20之間,當(dāng)任意一個(gè)數(shù)超出范圍時(shí),拋出自己的異常。實(shí)驗(yàn)步驟:步驟(1):基于系統(tǒng)異常類Exception,定義自己的異常類NumberRangeException。步驟(2):定義包含main方法的Application類SelfException。步驟(3):在SelfException類中添加公共方法:Public static int selfExceptionTest(int int1,int int2) throws NumberRangeException使之能在求int1,int2兩個(gè)數(shù)和之前檢查兩個(gè)數(shù)的數(shù)值范圍,若符合

7、條件則求和,否則拋出異常NumberRangeException。步驟(4):在main方法中調(diào)用selfExceptionTest方法。步驟(5):保存文件為SelfException.java,然后編譯并調(diào)試程序。步驟(6):修改main方法中調(diào)用selfExceptionTest方法的實(shí)參,看看程序的運(yùn)行結(jié)果有什么不同。程序代碼:class NumberRangeException extends Exceptionstatic int a,b;public NumberRangeException(String message,int a,int b)super(message);th

8、is.a=a;this.b=b;主 要 實(shí) 驗(yàn) 記 錄 及 個(gè) 人 小 結(jié) (包括部分實(shí)驗(yàn)源程序、調(diào)試結(jié)果及實(shí)驗(yàn)結(jié)果分析等) public class SelfException public static void selfExceptionTest(int a,int b) throws NumberRangeExceptionif(a20)|(b20)throw new NumberRangeException(數(shù)值超出指定范圍, a, b);System.out.println(a=+a+ +b=+b+n+sum=+(a+b); public void GetSum()tryselfE

9、xceptionTest(1,100);catch(NumberRangeException e)System.out.println(a=+NumberRangeException.a+ b=+NumberRangeException.b+n+e);public static void main(String args)SelfException num=new SelfException();num.GetSum();程序截圖:(第一次設(shè)a=1,b=100;第二次設(shè)a=15,b=15。)主 要 實(shí) 驗(yàn) 記 錄 及 個(gè) 人 小 結(jié) (包括部分實(shí)驗(yàn)源程序、調(diào)試結(jié)果及實(shí)驗(yàn)結(jié)果分析等)思考題:1、

10、翻譯下列常用異常類的描述信息OutOfMemoryError A class instance creation expression, array creation expression , or string concatenation operatior expression throws an OutOfMemoryError if there is insufficient memory available.如果沒有足夠的可用內(nèi)存,一個(gè)類的實(shí)例創(chuàng)建表達(dá)式,數(shù)組創(chuàng)建表達(dá)式,或字符串串聯(lián)操作表達(dá)會(huì)拋出一個(gè)OutOfMemoryError。 NegativeArraySizeExcepti

11、on An array creation expression throws a NegativeArraySizeException if the value of any dimension expression is less than zero.一個(gè)數(shù)組創(chuàng)建表達(dá)式會(huì)拋出一個(gè)NegativeArraySizeException如果任何維度表達(dá)的數(shù)值小于零。NullPointerException A field access throws a NullPointerException if the value of the object referenceexpression is nu

12、ll. A method invocation expression that invokes an instance method throws a NullPointerException if the target reference is null. An array access throws a NullPointerException if the value of the array referenceexpression is null. 一個(gè)字段訪問拋出NullPointerException如果對象的值引用表達(dá)式是null。一個(gè)方法調(diào)用,調(diào)用一個(gè)實(shí)例方法表達(dá)拋出NullP

13、ointerException如果目標(biāo)參考是null。一個(gè)數(shù)組訪問拋出NullPointerException如果值的數(shù)組引用表達(dá)式是null。ArrayIndexOutOfBoundsException An array access throws an ArrayIndexOutOfBoundsException if the value of the array index expression is negative or greater than or equal to the length of the array. 一個(gè)數(shù)組訪問拋出一個(gè)ArrayIndexOutOfBoundsE

14、xception如果值的數(shù)組索引表達(dá)式是負(fù)面的或大于或等于數(shù)組的長度。主 要 實(shí) 驗(yàn) 記 錄 及 個(gè) 人 小 結(jié) (包括部分實(shí)驗(yàn)源程序、調(diào)試結(jié)果及實(shí)驗(yàn)結(jié)果分析等)ClassCastException Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException: Object x = new Integer(0)

15、; System.out.println(String)x);拋出指示代碼試圖創(chuàng)建一個(gè)對象的一個(gè)子類,它不是一個(gè)實(shí)例。例如,下面的代碼生成一個(gè)ClassCastException異常: Object x = new Integer(0); System.out.println(String)x)ArithmeticException Thrown when an exceptional arithmetic condition has occurred. For example, an integer divide by zero throws an instance of this clas

16、s. 一個(gè)非凡的算術(shù)條件時(shí)拋出發(fā)生。例如,一個(gè)整數(shù)”除以零”拋出一個(gè)這個(gè)類的實(shí)例。ArrayStoreExceptionAn assignment to an array component of reference type throws an ArrayStoreException when the value to be assigned is not compatible with the component type of the array.一個(gè)賦值數(shù)組組件的引用類型拋出一個(gè)ArrayStoreException當(dāng)價(jià)值分配是不兼容的組件類型數(shù)組。NoSuchFieldExcepti

17、onSignals that the class doesnt have a field of a specified name.提示:在類中沒有一個(gè)方法指定為該名稱。NoSuchMethodExceptionThrown when a particular method cannot be found.拋出它,當(dāng)該方法不能被找到的時(shí)候。NumberFormatExceptionThrown to indicate that the application has attempted to convert a string to one of the numeric types, but th

18、at the string does not have the appropriate format. 拋出它,當(dāng)確定應(yīng)用程序試圖將字符串轉(zhuǎn)換成數(shù)值類型之一,但是,字符串沒有適當(dāng)主 要 實(shí) 驗(yàn) 記 錄 及 個(gè) 人 小 結(jié) (包括部分實(shí)驗(yàn)源程序、調(diào)試結(jié)果及實(shí)驗(yàn)結(jié)果分析等)的格式。FileNotFoundExceptionSignals that an attempt to open the file denoted by a specified pathname has failed.提示:試圖打開文件,表明一個(gè)指定的路徑名已經(jīng)失敗了。IOExceptionSignals that an I/O

19、 exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. 提示:一個(gè)I / O信號發(fā)生某種異常。這個(gè)類是一般類的異常產(chǎn)生的失敗或中斷I / O操作。2、編寫一個(gè)程序,用于根據(jù)用戶輸入的命令行參數(shù)數(shù)量來計(jì)算長方形、正方形、三角形的面積。如果輸入的參數(shù)個(gè)數(shù)為1、2、3則它們應(yīng)分別對應(yīng)正方形、長方形、三角形,如果參數(shù)值為0,則異常處理方法顯示錯(cuò)誤消息。提示:自己定義一個(gè)異常類,表示參數(shù)個(gè)數(shù)0這

20、一異常。然后定義一個(gè)抽象的父類,并提供一個(gè)抽象的方法area(),再派生出三個(gè)子類,重寫area方法,最后在main方法中編寫測試邏輯。import java.util.Scanner;class MyException extends Exception public void MyException(int i) if(i=0) System.out.println(輸入出錯(cuò)!請重新輸入:); abstract class area abstract void area();class RectangleArea extends area int area(int a,int b) int

21、 area=a*b;return area;主 要 實(shí) 驗(yàn) 記 錄 及 個(gè) 人 小 結(jié) (包括部分實(shí)驗(yàn)源程序、調(diào)試結(jié)果及實(shí)驗(yàn)結(jié)果分析等)class SquareArea extends area int area(int a) int area=a*a;return area; void area() class TriangleArea extends area double area(int a,int b,int c) double p=0.5*(a+b+c);double area=Math.sqrt( p*(p-a)*(p-b)*(p-c);return area; void are

22、a()public class CountArea extends MyException static int a,b,c,i; static int s=a,b,c; static MyException me=new MyException(); public static void main(String args) throws MyException Scanner sca=new Scanner(System.in); if(sca.equals(null) me.MyException(0); elsewhile(true) System.out.println(輸入若干整數(shù),用空格隔開,以Enter結(jié)束); String num=sca.nextLine(); num=num.toLowerCase(); if(num.equals(Enter) break; String strs=num.split( ); i=strs.length; if(i=1) SquareArea sa=new SquareArea();主 要 實(shí) 驗(yàn) 記 錄 及 個(gè) 人 小 結(jié) (包括部分實(shí)驗(yàn)源程序、調(diào)試結(jié)果及實(shí)驗(yàn)結(jié)果分析等)int area=sa.area(Integer.parseInt(strs0);System.out.println(正方形面積為+area);

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論