信息科学与工程学院《Java程序设计》上机实验报告专业班级姓名学号实验时间指导教师成绩实验名称实验四异常类的定义及处理实验目的1)了解异常处理方法。
2)熟悉并掌握常见异常的捕获方法。
3)熟悉JDK中已经定义的若干异常类的层次结构。
4)掌握自定义异常类的创建方法。
主要实验记录及个人小结(包括部分实验源程序、调试结果及实验结果分析等)1.编写程序实现如下功能:生成并捕获到NegativeArraySizeException和IndexOutOfBoundsException类型的异常,并显示捕获到的异常信息。
然后在此基础上生成并捕获到NullPointerException类型的异常,并显示捕获到的异常信息。
1)实验源程序:public class TestException {public static void main(String[] args){outofBound();arraySize();nullPointer();}static void arraySize(){try{int p=-5;int test1[]=new int[p]; //不一定要是-5,只要小于0的就能捕获到无效数组长度的异常}}catch (NegativeArraySizeException e) {System.out.println("捕获异常:\n"+e);System.out.println(e.toString()); //把捕获的错误异常转变为字符串类型,传给println()方法,用以输入。
}finally{System.out.println("捕获完成\n"); } }static void outofBound(){try {int test2[]=new int[10];test2[100]=1; //超出数组长度} catch (ArrayIndexOutOfBoundsException e) {System.out.println("捕获异常:\n"+e);System.out.println(e.toString());}finally{System.out.println("捕获完成\n\n");}}static void nullPointer(){try {int test3[]=null;test3[1]=100; //空指针无法赋值} catch (NullPointerException e) {主要实验记录及个人小结(包括部分实验源程序、调试结果及实验结果分析等) System.out.println("捕获异常:\n"+e);System.out.println(e.toString());}finally{System.out.println("捕获完成 \n\n")}}}2)调试结果:将outofBound()方法中捕获异常的语句注释掉,重新编译程序,看看会不会有什么语法错误?如果没错误,执行程序看结果有什么不同?static void outofBound(){try { int test2[]=new int[10];test2[100]=1; //超出数组长度} //catch (ArrayIndexOutOfBoundsException e) {// System.out.println("捕获异常:\n"+e);// System.out.println(e.toString());// }主要实验记录及个人小结(包括部分实验源程序、调试结果及实验结果分析等)将array方法重新定义为如下形式:void arraySize() throws NegativeArraySizeException{……}然后修改arraySize方法中捕获NegativeArraySizeException异常的语句执行部分。
public class TestException {public static void main(String[] args){try{outofBound();arraySize();nullPointer();}catch(NegativeArraySizeException e){System.out.println(e.toString());}}......(中间代码不变)static void nullPointer(){try {int test3[]=null;test3[1]=100; //空指针无法赋值} catch (NullPointerException e) {System.out.println("捕获异常:\n"+e);System.out.println(e.toString());}finally{System.out.println("捕获完成 \n\n");}}}主要实验记录及个人小结(包括部分实验源程序、调试结果及实验结果分析等)2.编写程序实现如下功能:计算两个数之和,参与求和运算的每个数的值都必须在10-20之间,当任意一个数超出范围时,抛出自己的异常。
1)实验源程序:public class SelfException{public static void selfExceptionTest(int a,int b) throws NumberRangeException{int answer;if(( a>10& b <20)&( a>10& b<20)){ answer=a+b;System.out.println("两数之和为:"+answer);}e lsethrow new NumberRangeException("错误");}public void manager(){try{selfExceptionTest(55,100);}catch(NumberRangeException e){System.out.println("输入数值超出范围,应该为10-20之间的整数");}}public static void main(String args[]){SelfException s=new SelfException();s.manager();}}class NumberRangeException extends Exception{public NumberRangeException(String message){super(message);}}2)调试结果:3.思考题:1)翻译下列常用异常类的描述信息OutOfMemoryErrorA class instance creation expression, array creation expression , or string concatenation operatior expression throws an OutOfMemoryError if there is insufficient memory available.翻译:内存溢出错误如果没有足够的内存空间,一个类的实例创建的表达式,数组创建的表达式,或者一个字符串连接符表达式抛出一个内存溢出的错误.NegativeArraySizeExceptionAn array creation expression throws a NegativeArraySizeException if the value of any dimension expression is less than zero.翻译:创建大小为负的数组的异常任何维度表达式的值小于零,则数组创建的表达式抛出一个创建大小为负的数组的异常.NullPointerExceptionA field access throws a NullPointerException if the value of the object reference expression is null.A method invocation expression that invokes an instance method throws a NullPointerException if the target reference is null.An array access throws a NullPointerException if the value of the array reference expression is null.翻译:空指针异常如果对象引用表达式的值是空的,一个字段的访问抛出一个空指针异常。
如果是空的目标参考,一个方法调用表达式调用一个实例方法抛出一个空指针异常,。
如果数组引用表达式的值是空,数组访问抛出一个空指针异常。
ArrayIndexOutOfBoundsExceptionAn array access throws an ArrayIndexOutOfBoundsException if the value of the array index expression is negative or greater than or equal to the length of the array. 翻译:用非法索引访问数组时抛出的异常异常数组访问抛出一个用非法索引访问数组时抛出的异常,如果数组索引表达式的值是负或大于等于数组的长度。
ClassCastExceptionThrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:Object x = new Integer(0);System.out.println((String)x);翻译:强制类型转换错误异常异常抛出,以指示代码试图将对象转换为一个子类,它不是一个实例。
例如,以下代码将生成一个ClassCastException:对象x =新的整数(0);输出((字符串)X);ArithmeticExceptionThrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero" throws an instance of this class.翻译:算数异常当发生了一个特殊的算术条件时抛出。