预览加载中,请您耐心等待几秒...
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

亲,该文档总共12页,到这已经超出免费预览范围,如果喜欢就直接下载吧~

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

实验7异常解决与异常类一.实验目及实验环境1理解Java异常基本概念和解决机制。2掌握Java异常解决办法(抛出和捕获异常;try、throw、throws、catch语句和finally用法)3理解异常类作用,掌握创立异常类办法。二.实验内容1基本内容(实验前请及时熟悉如下有关内容)1)Java异常解决机制2)异常类应用3)常用异常(除数为零等)4)多异常解决5)由办法抛出异常6)必要要捕获异常2综合实验:2.1(Y.DanielLiang英文版第10版P488:12.2*(InputMismatchException))(Y.DanielLiang英文版八版P456:13.2*)(NumberFormatException)Writeaprogramthatpromptstheusertoreadtwointegersanddisplaystheirsum.Yourprogramshouldprompttheusertoreadthenumberagainiftheinputisincorrect.2.2(Y.DanielLiang英文版第10版P488:12.3*)(Y.DanielLiang英文版八版P456:13.3*)(ArrayIndexOutBoundsException)Writeaprogramthatmeetsthefollowingrequirements:■Createanarraywith100randomlychosenintegers.■Prompttheusertoentertheindexofthearray,thendisplaythecorrespondingelementvalue.Ifthespecifiedindexisoutofbounds,displaythemessageOutofBounds.2.3(Y.DanielLiang英文版第10版P488:12.4*)(Y.DanielLiang英文版八版P456:13.4*)(IllegalArgumentException)ModifytheLoanclassinListing10.2tothrowIllegalArgumentExceptioniftheloanamount,interestrate,ornumberofyearsislessthanorequaltozero.2.4(Y.DanielLiang英文版第10版P488:12.5*)(Y.DanielLiang英文版八版P456:13.5*)(IllegalTriangleException)Exercise11.1definedtheTriangleclasswiththreesides.Inatriangle,thesumofanytwosidesisgreaterthantheotherside.TheTriangleclassmustadheretothisrule.CreatetheIllegalTriangleExceptionclass,andmodifytheconstructoroftheTriangleclasstothrowanIllegalTriangleExceptionobjectifatriangleiscreatedwithsidesthatviolatetherule,asfollows:/**Constructatrianglewiththespecifiedsides*/publicTriangle(doubleside1,doubleside2,doubleside3)throwsIllegalTriangleException{//Implementit}三、程序清单及成果:1,package异常解决;importjava.util.Scanner;publicclasstext1{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubinta,b;Scanners=newScanner(System.in);System.out.println("readtwointegers:");while(true){try{a=s.nextInt();b=s.nextInt();System.out.printf("%d",a+b);}catch(Exceptione){System.out.println("readthenumberagain");s.nextLine();}}}}2package异常解决;importjava.util.Scanner;publicclasstext2{publicstaticvoidmain(String[]ar