实验二 泛型异常处理 实验报告
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验二泛型异常处理实验报告
一、实验目的
1. 理解使用泛型、枚举的目的。
2. 掌握异常类、异常类的结构及自定义异常类的编程方法。
二、实验要求
1. 应用泛型编写简单的应用程序;
2. 自定义异常类的编写;
3. 异常类中try, catch, throw, throws 的编写方法。
三、实验内容
1. 阅读以下程序,写出运行结果。同时,把程序改写为使用泛型的程序。对比两个程序并说出泛型的优点。
public class GenericityDemo {
public static void main(String[] args) {
// 定义类Genericity的一个Integer版本
Genericity intobj = new Genericity(new Integer(88));
intobj.showType();
int i = (Integer) intobj.getobj();
System.out.println("value= " + i);
System.out.println("----------------------------------");
// 定义类Genericity的一个String版本
Genericity strobj = new Genericity("Hello Genericity!");
strobj.showType();
String s = (String) strobj.getobj();
System.out.println("value= " + s);
}
}
class Genericity {
private Object obj; // 定义通用类型成员
public Genericity(Object obj) {
this.obj = obj;
}
public Object getobj() { return obj;}
public void setobj(Object obj) {this.obj = obj;}
public void showType() {
System.out.println("T实际类型是: " + obj.getClass().getName());
}
}
2. 阅读程序并分析其输出结果:
public class Exceptiontest {
public static void mb_createException() {
throw new ArrayIndexOutOfBoundsException();
}// 方法mb_createException结束
public static void mb_method() {
try {
mb_createException();
System.out.print("try…\n");
} catch (ArithmeticException e) {
System.out.print("catch arithmeticException\n");
} finally {
System.out.print("execute finally\n");
}// try-catch-finally结构结束
System.out.print("mb_method end\n");
}// 方法me_method结束
public static void main(String args[]) {
try {
mb_method();
} catch (Exception e) {
System.out.print("catch exception\n");
}// try-catch结构结束
System.out.print("main end\n");
}// 方法main结束
}
3. 编程实现输入二个整数,输出它们的商(整除的结果)。要求使用异常机制:当除数为0时,输出除数为0。当输入的数据出现其它字符时,重新输入数据。
四、实验结果
1. 实验(1)的运行结果如下:
把实验(1)的程序改写为泛型程序代码如下:
public class GenericityNew
private T obj;
public GenericityNew (T obj){this.obj = obj;}
public T getobj() {return this.obj;}
public void setobj(T obj) {this.obj = obj;}
public void showType() {
System.out.println("T的实际类型是: "+ obj.getClass().getName());
}
public static void main(String[] args) {
GenericityNew
new GenericityNew
intobj.showType();
int i = intobj.getobj();
System.out.println("value= " + i);
System.out.println("----------------------------------");
GenericityNew
new GenericityNew
strobj.showType();
String s = strobj.getobj();
System.out.println("value= " + s);
}
}
相比起原来的那个程序,泛型类对setobj方法的参数进行了限定,getobj的返回值也不需要进行强制类型转换,这样能够确保程序中不会出现ClassCastException异常。
2. 实验(2)的结果如下:
在main函数中,调用mb_method方法,mb_method方法中调用mb_createException 方法,这个方法会抛出一个ArrayIndexOutOfBoundsException异常,而这个异常没有被mb_method方法中的try…catch 语句捕获异常,但无论如何,finally中的代码始终会被执行。所以输出了execute finally,然后就把异常继续往上一层的方法,也就是main方法抛;所以mb_method方法中的System.out.print("mb_method end\n");语句没有被执行。ArrayIndexOutOfBoundsException异常被main函数中的try…catch语句捕获,所以System.out.print("catch exception\n");语句被执行,异常已经处理完成,程序会继续往下执行,所以main end也就被输出。