2022年异常类的定义及处理借鉴 .pdf
一、实验名称异常类的定义及处理二、实验目的1)了解异常处理方法。2)熟悉并掌握常见异常的捕获方法。3)熟悉 JDK中已经定义的若干异常类的层次结构。4)掌握自定义异常类的创建方法。三、实验记录1.编写程序实现如下功能:生成并捕获到NegativeArraySizeException和 IndexOutOfBoundsException类型的异常,并显示捕获到的异常信息。然后在此基础上生成并捕获到NullPointerException类型的异常,并显示捕获到的异常信息。步骤(1):编写一个包含main 方法的 Application类 TestException,然后定义一个方法void arraySize()生成并捕获NegativeArraySizeException异常。步骤(2):添加一个方法void outofBound()生成并捕获IndexOutOfBoundsException异常。步骤(3):添加一个方法void nullPointer()生成并捕获IndexOutOfBoundsException异常。步骤(4):在 main 方法中分别调用以上三个方法。步骤(5):将文件保存为TestException.java,然后编译、调试应用程序。步骤(6):将 outofBound()方法中捕获异常的语句注释掉,重新编译程序,看看会不会有什么语法错误?如果没错误,执行程序看结果有什么不同?步骤(7):将 array 方法重新定义为如下形式:void arraySize()throws NegativeArraySizeException 然后修改arraySize方法中捕获NegativeArraySizeException异常的语句执行部分。源程序如下:class TestException publicstaticvoid main(String args)try outofBound();arraySize();nullPointer();catch(NegativeArraySizeException e)名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 7 页 -System.out.println(e.toString();staticvoid arraySize()try int a;a=newint-3;catch(NegativeArraySizeException e)System.out.println(Error:Negative Array Size);staticvoid outofBound()try int i;int a;a=newint5;for(i=0;i6;i+)ai=i;System.out.println(a+i+=+ai);catch(IndexOutOfBoundsException e)System.out.println(Error:Index Out Of Bounds);staticvoid nullPointer()try String s=null;System.out.println(s.length();catch(NullPointerException e)System.out.println(Error:Null Pointer);/*static void arraySize()throws NegativeArraySizeException try int a;a=new int-3;catch(NegativeArraySizeException e)throw e;*/运行结果如下:(1)名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 7 页 -(2)注释掉 outofBound()方法中捕获异常的语句(3)重新定义 array 方法2.编写程序实现如下功能:计算两个数之和,参与求和运算的每个数的值都必须在10-20 之间,当任意一个数超出范围时,抛出自己的异常。步骤(1):基于系统异常类Exception,定义自己的异常类NumberRangeException。步骤(2):定义包含main 方法的 Application类 SelfException。步骤(3):在 SelfException类中添加公共方法:Public static int selfExceptionTest(int int1,int int2)throws NumberRangeException 使之能在求int1,int2两个数和之前检查两个数的数值范围,若符合条件则求和,否则抛出异常 NumberRangeException。步骤(4):在 main 方法中调用selfExceptionTest方法。步骤(5):保存文件为SelfException.java,然后编译并调试程序。步骤(6):修改 main 方法中调用selfExceptionTest方法的实参,看看程序的运行结果有什么不同。源程序如下:class NumberRangeException extends Exception privateintint1,int2;intresult;public NumberRangeException(String message,int int1,int int2)super(message);this.int1=int1;this.int2=int2;名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 7 页 -publicclass SelfException publicstaticvoidselfExceptionTest(intint1,intint2)throwsNumberRangeException if(int120)|(int220)thrownew NumberRangeException(超出指定范围,int1,int2);System.out.println(int1=+int1+int2=+int2+n+result=+(int1+int2);publicvoid getSum()try selfExceptionTest(25,15);catch(NumberRangeException e)System.out.println(数值超出指定范围);publicstaticvoid main(String args)SelfException num=new SelfException();num.getSum();运行结果如下:(1)(2)修改参数后3.编写一个程序,用于根据用户输入的命令行参数数量来计算长方形、正方形、三角形的面积。如果输入的参数个数为1、2、3 则它们应分别对应正方形、长方形、三角形,如果参数值为0,则异常处理方法显示错误消息。提示 :自己定义一个异常类,表示参数个数0 这一异常。然后定义一个抽象的父类,并提供一个抽象的方法 area(),再派生出三个子类,重写area 方法,最后在main 方法中编写测试逻辑。源程序如下:名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 7 页 -import java.io.*;class NullParameterException extends Exception NullParameterException(String Message)super(Message);class TestArea publicstaticvoid main(String args)Shape sh=null;String s=,s1=,s2=,s3=;int n=0;double a=0,b=0,c=0;try BufferedReader in=newBufferedReader(newInputStreamReader(System.in);System.out.print(Please input the number of parameter(input0,1,2or3):);s=in.readLine();n=Integer.parseInt(s);catch(IOException e)tryBufferedReader in=newBufferedReader(newInputStreamReader(System.in);switch(n)case 1: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(s1);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.parseDouble(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 Triangle(a,b,c);break;名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 7 页 -case 0:thrownew NullParameterException(The number of parameter is null!);System.out.println(sh.area();catch(Exception e)System.out.println(e.toString();abstractclass Shape abstractdouble area();class Triangle extends Shape doublea,b,c;Triangle(double a,double b,double c)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 Shape doublel,w;Rectangle(double l,double w)this.l=l;this.w=w;double area()return(l*w);class Square extends Shape doublel;Square(double l)this.l=l;double area()returnl*l;运行结果如下:名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 7 页 -(1)(2)(3)(4)四、个人小结本次实验是编写三个java 程序实现简单的功能并从中认识java 异常类的定义及处理,其中第一个和第二个实验要求检验自己定义的异常类,以修改一些参数来达到目的,修改参数后可以看到运行结果不同,这样就使我们深刻认识到在java 中捕获异常与处理异常的重要性。实验是在java 集成开发环境Myeclipse中完成的,在Myeclipse 中完成java 源程序的编辑、编译与运行。实验中遇到过很多问题,而Myeclipse能够较准确的指出错误并提出修改建议,在不断的修改后,终于使得所写的程序没有语法等错误,但在运行时却不能正常运行,经检查发现是方法的调用以及变量的初始化出现了问题,改正后程序正常运行出现正确的结果。通过这次上机,我对 java 面向对象的程序设计的概念有了进一步的认识,深化了对课本知识的了解与认识,同时在本次上机实验中我尝试了使用输入输出流,从键盘输入参数,因此也进一步学会了一些编程方法与技巧,希望经过以后的上机来更好的掌握java。名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 7 页 -