實(shí)驗(yàn)指導(dǎo)書:實(shí)驗(yàn)7-異常處理與異常類_第1頁(yè)
實(shí)驗(yàn)指導(dǎo)書:實(shí)驗(yàn)7-異常處理與異常類_第2頁(yè)
實(shí)驗(yàn)指導(dǎo)書:實(shí)驗(yàn)7-異常處理與異常類_第3頁(yè)
實(shí)驗(yàn)指導(dǎo)書:實(shí)驗(yàn)7-異常處理與異常類_第4頁(yè)
實(shí)驗(yàn)指導(dǎo)書:實(shí)驗(yàn)7-異常處理與異常類_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、實(shí)驗(yàn)7 異常處理與異常類一. 實(shí)驗(yàn)?zāi)康募皩?shí)驗(yàn)環(huán)境 1理解Java異常的基本概念和處理機(jī)制。 2掌握J(rèn)ava異常處理的方法(拋出和捕獲異常;try、throw、throws、catch語(yǔ)句和finally的用法) 3理解異常類的作用,掌握創(chuàng)建異常類的方法。二. 實(shí)驗(yàn)內(nèi)容 1 基本內(nèi)容(實(shí)驗(yàn)前請(qǐng)及時(shí)熟悉如下相關(guān)內(nèi)容)1)Java的異常處理機(jī)制2)異常類的應(yīng)用3)常見(jiàn)的異常(除數(shù)為零等)4)多異常處理5)由方法拋出異常6)必須要捕獲的異常 2 綜合實(shí)驗(yàn):2.1 (Y. Daniel Liang英文版第10版P488:12.2* (InputMismatchException) (Y. Daniel

2、Liang英文版八版P456:13.2*) (NumberFormatException) Write a program that prompts the user to read two integers and displays their sum. Your program should prompt the user to read the number again if the input is incorrect.2.2 (Y. Daniel Liang英文版第10版P488:12.3* ) (Y. Daniel Liang英文版八版P456:13.3*) (ArrayIndex

3、OutBoundsException) Write a program that meets the following requirements: Create an array with 100 randomly chosen integers. Prompt the user to enter the index of the array, then display the corresponding element value. If the specified index is out of bounds, display the message Out of Bounds.2.3

4、(Y. Daniel Liang英文版第10版P488:12.4* ) (Y. Daniel Liang英文版八版P456:13.4*) (IllegalArgumentException) Modify the Loan class in Listing 10.2 to throw IllegalArgumentException if the loan amount, interest rate, or number of years is less than or equal to zero.2.4 (Y. Daniel Liang英文版第10版P488:12.5* ) (Y. Dani

5、el Liang英文版八版P456:13.5*) (IllegalTriangleException) Exercise 11.1 defined the Triangle class with three sides. In a triangle, the sum of any two sides is greater than the other side. The Triangle class must adhere to this rule. Create the IllegalTriangleException class, and modify the constructor of

6、 the Triangle class to throw an IllegalTriangleException object if a triangle is created with sides that violate the rule, as follows:/* Construct a triangle with the specified sides */public Triangle(double side1, double side2, double side3)throws IllegalTriangleException / Implement it三、程序清單及結(jié)果:1,

7、package 異常處理;import java.util.Scanner;public class text1 public static void main(String args) / TODO Auto-generated method stubint a,b;Scanner s=new Scanner(System.in);System.out.println(read two integers:);while(true)try a=s.nextInt();b=s.nextInt();System.out.printf(%d,a+b);catch(Exception e)System

8、.out.println(read the number again);s.nextLine();2package 異常處理;import java.util.Scanner;public class text2 public static void main(String args) / TODO Auto-generated method stubint sz=new int100;for(int i=0;isz.length;i+)szi=(int)Math.random()*100;System.out.println(enter the index of the array:);Sc

9、anner s=new Scanner(System.in);int n=s.nextInt();try System.out.println(szn);catch(ArrayIndexOutOfBoundsException e)System.out.println(System.out.println);3import java.lang.Math;import java.io.BufferedReader;import java.io.InputStreamReader;/自定義異常類,繼承與異常類Exceptionclass MyException extends ExceptionM

10、yException(String cause,double m)/若出現(xiàn)異常,每處理一次異常,標(biāo)記flag-shiyan3.flag-;/輸出出錯(cuò)提示語(yǔ)句System.out.println(cause+的值為:+m+小于等于零);class DaiKuan/定義變量貸款額、年利率、貸款年限、月還款額、總還款額double DaiKuanE,NianLiLv,DaiKuanNianShu,YueKuanHuanE,ZongKuanHuanE;public void DaiKuanJiSuan()while(shiyan3.flag=3)try/依次輸入貸款額、年利率、貸款年限Buffered

11、Reader br=new BufferedReader(new InputStreamReader(System.in);System.out.println(請(qǐng)輸入貸款額:);System.out.println(請(qǐng)輸入年利率:);System.out.println(請(qǐng)輸入貸款年限:);DaiKuanE=Double.parseDouble(br.readLine();NianLiLv=Double.parseDouble(br.readLine(); DaiKuanNianShu=Double.parseDouble(br.readLine();catch(Exception e)tr

12、y/若貸款額不大于零,拋出異常if(DaiKuanE=0)throw new MyException(貸款額,DaiKuanE);catch (MyException e)try/若年利率不大于零,拋出異常if(NianLiLv=0)throw new MyException(年利率,NianLiLv);catch (MyException e)try/若貸款年限不大于零,拋出異常if(DaiKuanNianShuside3&(side1+side3)side2&(side3+side2)side1)this.side1 = side1; this.side2 = side2; this.side3 = side3; elsethrow new IlleagalTriangleException(side1,side2,side3);public String tostring() return 合法;心得:實(shí)驗(yàn)過(guò)程中try塊中的語(yǔ)句塊只要發(fā)生異常就立刻爆出異常對(duì)象,然后被對(duì)應(yīng)的catch塊接住,也就是try之后的代碼不會(huì)在程序中執(zhí)行,當(dāng)然也不會(huì)有任何的作用。當(dāng)catch記住對(duì)象后,會(huì)執(zhí)行相應(yīng)的catch之后的代碼,而無(wú)論是否發(fā)生

溫馨提示

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

評(píng)論

0/150

提交評(píng)論