Java语言 实验题目_第1页
Java语言 实验题目_第2页
Java语言 实验题目_第3页
Java语言 实验题目_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第五章Java语言异常处理-实验题目、实验目的1. 掌握Java的异常处理机制。2. 掌握try、catch、throws和fin ally这5个关键字的用法。3. 掌握成员的几个异常类。4. 掌握自定义异常的使用。1、实验内容1 编写程序,从键盘上输入两个数。将第一个数除以第二个数并显示结果;处理所有可能的异常。2 写出下面程序运行的结果:自定义异常class MyException extends ExceptiontString message;MyException(String str irie3sage=3tr;public String get-Megsage () return

2、 message;abstract class ABCabstract :Ln 七 f (int int y) throws My Ex c:mpt;i on;class SuhABC extends ABCint f(int int y) throws MvExcepcionif(x99| |y99)throw new MyExc亡pt ioti (乘数超过! N); return x*y;public class HyEKceptionDemo public static void main(String args ) ABC aloe;afocnew SubABC();Systerti

3、总卫pirinuJ.n (abc (12,8); Sys 匸 Em .out* print in. (akic - f C120, 3);catch(MyException e)SysLeirou t, print In (e. getMeggage ();3 .写出下列程序的输出结果。/TestO1.javapublic class TestO1public static void main( Stri ngargs) tryfor(int i=0 ; i3; i+)System, out. println( ” The value of i :” +i);System, out. prin

4、tln(5 /i);catch(ArithmeticExcepti on e)System. out. println( ” Division by zero );for(int i=0 ; i3; i+)trySystem. Out. println( ” The value of i : +i);System. out. println(5 / i);catch(ArithmeticExcepti on e)System. out. println( ” Division by zero .” );4 .有下列程序:/Test02. javapublic class Test02publi

5、c static void main(strin 虻args)String foo=args1 ;String bar=args-2 ;String baz=args-3 ;若用下面方式执行该程序,则baz的值为()。java Test Red Gree n Blue(G)程序抛出异常(A) “”(B)null(C)Red ”(D) ”Blue ”(E) ”Green” (F)代码不能编译5. 有下列程序:/Fool.javapublic class Foo1 public static void main( Stri ng args)tryreturn; fin ally System.ou

6、t.pri ntl n( ” Fin ally ; ”)该程序的输出结果为()。(A)没有任何输出(B)输出 Fin ally ”(C)编译错误(D)抛出异常6 .有下列程序:Foo2.javapublic class Foo2public static void main( Stri ng args)trySystem. exit(O);fin ally System.out.pri ntln( “ Fin ally ” );该程序的输出结果为()。(A)没有任何输出(B)输出“ Finally ”(C)编译错误 (D)抛出异常7 .修改下列程序的错误之处。|TesO3.javaclass

7、Test03public static void main( Stri ng args )tryint a=10;System.out.println(a / 0);catch(Exceptio n e)Sysem.out.println( An exception occurred.);catch(ArithmeticExcepti on ae) System.out.println( Arithmetic exception occurred. ” ;)&写出下列程序的运行结果。/ Tes04. javaclass Test04public static void main( Stri n

8、g args)trymb_method();catch(Excepti on e)System.out.pri nt(, m?);System.out.print( ,n?);static void mb_createExcepti on()throw new Arra yin dexOutOfB oun dsExceptio n();static void mb_method()try mb_createExcepti on(); System.out.print( a“);catch(ArithmeticExcepti on e) System.out.print( b); fin allySystem.out.print( F);System.out.print( d);9编写一个程序,要求从键盘输入一个double型的圆的半径,计算并精出其面积。测试当输入的数据不是double型数据(如字符串“ abc”)会产生什么结果,怎样处理。10 .编写一个应用程序,在main()方法中使用ry块抛出一个 Exception类的对象,为Exception的构造方法提供一个字符串参数,在catch块内捕获该异常并打印出字符串参数。添加一个fin ally块并打印一条消息。11 .创建一个自定义的异常类,该类继承Exception类,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论