异常类的定义及处理_第1页
异常类的定义及处理_第2页
异常类的定义及处理_第3页
异常类的定义及处理_第4页
异常类的定义及处理_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

1、一、实验名称异常类的定义及处理二、实验目的1) 了解异常处理方法。2) 熟悉并掌握常见异常的捕获方法。3) 熟悉JDK中已经定义的若干异常类的层次结构。4) 掌握自定义异常类的创建方法。三、实验记录1.编写程序实现如下功能:生成并捕获到NegativeArraySizeException和IndexOutOfBoundsException类型的异常,并显示捕获到的异常信息。然后在此基础上生成并捕获到NullPointerException类型的异常,并显示捕获到的异常信息。步骤(1):编写一个包含main方法的Application类TestException,然后定义一个方法void arr

2、aySize()生成并捕获NegativeArraySizeException异常。步骤(2):添加一个方法void outofBound()生成并捕获IndexOutOfBoundsException异常。步骤(3):添加一个方法void nullPointer()生成并捕获IndexOutOfBoundsException异常。步骤(4):在main方法中分别调用以上三个方法。步骤(5):将文件保存为TestException.java,然后编译、调试应用程序。步骤(6):将outofBound()方法中捕获异常的语句注释掉,重新编译程序,看看会不会有什么语法错误?如果没错误,执行程序看结

3、果有什么不同?步骤(7):将array方法重新定义为如下形式:void arraySize() throws NegativeArraySizeException然后修改arraySize方法中捕获NegativeArraySizeException异常的语句执行部分。源程序如下:class TestException public static void main(String args) tryoutofBound();arraySize();nullPointer();catch(NegativeArraySizeException e)System.out.println(e.toSt

4、ring();static void arraySize()tryint a; a=new int-3;catch(NegativeArraySizeException e)System.out.println("Error:Negative Array Size"); static void outofBound()tryint i;int a;a=new int5;for(i=0;i<6;i+) ai=i;System.out.println("a"+i+"="+ ai);catch(IndexOutOfBoundsExce

5、ption e)System.out.println("Error:Index Out Of Bounds"); static void nullPointer()tryString s=null;System.out.println(s.length();catch(NullPointerException e)System.out.println("Error:Null Pointer");/* static void arraySize() throws NegativeArraySizeException tryint a;a=new int-3

6、;catch(NegativeArraySizeException e)throw e;*/运行结果如下:(1)(2)注释掉outofBound()方法中捕获异常的语句(3)重新定义array方法2.编写程序实现如下功能:计算两个数之和,参与求和运算的每个数的值都必须在10-20之间,当任意一个数超出范围时,抛出自己的异常。步骤(1):基于系统异常类Exception,定义自己的异常类NumberRangeException。步骤(2):定义包含main方法的Application类SelfException。步骤(3):在SelfException类中添加公共方法:Public static

7、 int selfExceptionTest(int int1,int int2) throws NumberRangeException使之能在求int1,int2两个数和之前检查两个数的数值范围,若符合条件则求和,否则抛出异常NumberRangeException。步骤(4):在main方法中调用selfExceptionTest方法。步骤(5):保存文件为SelfException.java,然后编译并调试程序。步骤(6):修改main方法中调用selfExceptionTest方法的实参,看看程序的运行结果有什么不同。 源程序如下:class NumberRangeException

8、 extends Exceptionprivate int int1,int2;int result;public NumberRangeException(String message,int int1,int int2)super(message);1=int1;2=int2;public class SelfException public static void selfExceptionTest(int int1,int int2) throws NumberRangeExceptionif(int1<10|int1>20)|(int2&l

9、t;10|int2>20)throw new NumberRangeException("超出指定范围", int1, int2);System.out.println("int1="+int1+""+"int2="+int2+"n"+"result="+(int1+int2);public void getSum()tryselfExceptionTest(25,15);catch(NumberRangeException e) System.out.println(

10、"数值超出指定范围"); public static void main(String args) SelfException num=new SelfException(); num.getSum(); 运行结果如下:(1)(2)修改参数后3.编写一个程序,用于根据用户输入的命令行参数数量来计算长方形、正方形、三角形的面积。如果输入的参数个数为1、2、3则它们应分别对应正方形、长方形、三角形,如果参数值为0,则异常处理方法显示错误消息。提示:自己定义一个异常类,表示参数个数0这一异常。然后定义一个抽象的父类,并提供一个抽象的方法area(),再派生出三个子类,重写area

11、方法,最后在main方法中编写测试逻辑。源程序如下:import java.io.*;class NullParameterException extends ExceptionNullParameterException(String Message)super(Message);class TestAreapublic static void main(String args)Shape sh=null;String s="",s1="",s2="",s3=""int n=0;double a=0,b=0,c=

12、0;try BufferedReader in=new InputStreamReader(System.in);System.out.print("Please input the parameter(input0,1,2or3):");s=in.readLine(); n=Integer.parseInt(s); catch(IOException e)tryBufferedReader in=newInputStreamReader(System.in);switch(n)case 1:BufferedReader(new number of BufferedRead

13、er(new System.out.print("Please input a parameter:"); s1=in.readLine();a=Double.parseDouble(s1); System.out.print("The area of the square is:"); sh=new Square(a); break; case 2: System.out.print("Please input the 1st parameter:"); s1=in.readLine();a=Double.parseDouble(s

14、1); System.out.print("Please input the 2nd parameter:"); s2=in.readLine();b=Double.parseDouble(s2); System.out.print("The area of the rectangle is:"); sh=new Rectangle(a,b);break; case 3: System.out.print("Please input the 1st parameter:"); s1=in.readLine();a=Double.par

15、seDouble(s1); System.out.print("Please input the 2nd parameter:"); s2=in.readLine();b=Double.parseDouble(s2); System.out.print("Please input the 3rd parameter:"); s3=in.readLine();c=Double.parseDouble(s3); System.out.print("The area of the triangle is:"); sh=new Triangl

16、e(a,b,c);break; case 0:throw new NullParameterException("The number of parameter is null!");System.out.println(sh.area();catch(Exception e)System.out.println(e.toString();abstract class Shapeabstract double area();class Triangle extends Shapedouble a,b,c;Triangle(double a,double b,double c

17、)this.a=a;this.b=b;this.c=c;double area()double s;s=(a+b+c)/2;return(Math.sqrt(s*(s-a)*(s-b)*(s-c);class Rectangle extends Shapedouble l,w;Rectangle(double l,double w)this.l=l;this.w=w;double area()return (l*w);class Square extends Shapedouble l;Square(double l)this.l=l;double area()return l*l;运行结果如下:(1)(2)(3)(4)四、个人小结本次实验是编写三个java程序实现简单的功能并从中认识java异常类的定义及处理,其中第一个和第二个实验要求检验自己定义的异常类,以修改一些参数来达到目的,修改参数后可以看到运行结果不同,这样就使我们深刻认识到在java中捕获异常与处理异常的重要性。实验是在java集成开发环境Myec

温馨提示

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

评论

0/150

提交评论