《java---异常类匿名类实验报告.doc》由会员分享,可在线阅读,更多相关《java---异常类匿名类实验报告.doc(11页珍藏版)》请在三一文库上搜索。
1、实验十四 异常类、匿名类课程名称: 面向对象程序设计 实验项目名称: 实验十四 异常类、匿名类 【实验目的】1掌握Java异常类、嵌套类、匿名类。【实验内容】一、异常类实验1编译下面的程序,并回答问题。class ExceptionTest1 public static void main(String args) int a=0,b=0; try a = Integer.parseInt(args0); / 语句1 b = 10; / 语句2 catch(NumberFormatException e) a = 1; System.out.println(a); System.out.pri
2、ntln(b); (1)执行上述ExceptionTest1类(在命令提示符下敲入java ExceptionTest1 99) 运行结果:(2)执行上述ExceptionTest1类(在命令提示符下敲入java ExceptionTest1 a) 运行结果:(3)比较上面两次运行的结果,说明原因。(思考:执行语句1时发生异常,语句2执行没有?) 执行语句1时没有发生异常,语句2执行发生异常。因为在源代码中写有try a = Integer.parseInt(args0); / 语句1 b = 10; / 语句2 catch(NumberFormatException e) a = 1; 当在
3、运行代码时,若在文件名后面输入的是整数,则不会发生异常,try代码块的下面的代码继续执行,而当输入的是字符或是字符串时,try代码块会捕获错误的信息,但是并没有运行下面的代码,如果try代码块中添加了finally部分,将原来的System.out.println(a);System.out.println(b);填入到finally中,则无论有没有发生异常,都能执行到finally中的代码。2编译下面的程序,并回答问题。class ExceptionTest2 public static void main(String args) int a=0; try a = Integer.pars
4、eInt(args0); (1) 编译上述程序,是否出错?出错(2) 如果出错, 给出出错信息(即javac编译该文件出错信息): 分析为何出错? 出错原因:try和catch或finally是成对出现的,不能出现了try,而后面就没有了catch或者finally(3)如果将程序改写为: class ExceptionTest2 public static void main(String args) int a=0; try a = Integer.parseInt(args0); catch(NumberFormatException e) a = 1; 上述程序是否编译出错? 为什么?
5、上述程序中try与catch是成对出现的,所以编译不会出错。(4)如果将程序改写为:class ExceptionTest2 public static void main(String args) int a=0; try a = Integer.parseInt(args0); finally a = 1; 上述程序是否编译出错?不出错为什么?当出现了try时,后面要么接catch,要么接finally,但是不能这两个都不接。(4) 如果将程序改写为:class ExceptionTest2 public static void main(String args) int a=0; try
6、 a = Integer.parseInt(args0); catch(Exception e) a = 1; catch(NumberFormatException e) a = 2; 上述程序是否编译出错?分析为何出错, 如何修改程序使之正确?处理子类异常的catch代码块要写在前面,超类写在后面。class ExceptionTest2 public static void main(String args) int a=0; try a = Integer.parseInt(args0); catch(NumberFormatException e) a = 2; catch(Exce
7、ption e) a = 1; 3、编译下面的程序,并回答问题。class ExceptionTest3 public static void main(String args) check(-1); static void check(int i) if( i0) throw new Exception(); (1) 上述程序编译是否出错?出错 分析错误原因:(2)将上述程序修改为:class ExceptionTest3 public static void main(String args) check(-1); static void check(int i) throws Excep
8、tion if( i0) throw new Exception(); 那么,上述程序编译是否出错?出错分析出错原因: 4编译下面程序,回答问题。class ExceptionTest4 public static void main(String args) method3(); System.out.println(success!); static void method1() throw new Exception(); static void method2() method1(); static void method3() method2(); (1) 上述程序编译是否出错?出错
9、 编译错误是什么? (2)修正上述程序(通过两种方式:给方法添加throws子句,或者使用try-catch-finally)。class ExceptionTest4 public static void main(String args) method3(); System.out.println(success!); static void method1() try throw new Exception(); catch (Exception e) / TODO 自动生成 catch 块e.printStackTrace(); static void method2() method
10、1(); static void method3() method2(); (3)如果将方法method1中的throw new Exception();更改为:throw new RuntimeException();那么程序是否编译通过?通过如果编译正常,那么执行ExceptionTest4类,出现什么结果(是否会显示success字符串)?如何修改程序使得能够显示success字符串?class ExceptionTest4 public static void main(String args) try method3();catch(RuntimeException e) Syste
11、m.out.println(success!); static void method1() throw new RuntimeException(); static void method2() method1(); static void method3() method2(); 5、设计一个简单的类ExceptionTest, 在该类的main方法中实现:将输入参数args0转换为int类型的数字(要求使用包装类的数字转换方法,并通过try-catch-finally方式捕获并处理转换异常),然后显示这个数字的平方值。比如,在命令提示符下输入ExceptionTest 9 并回车,程序显
12、示81。class ExceptionTest public static void main(String args) int a=0; try a = Integer.parseInt(args0); / 语句1 catch(NumberFormatException e) a = 1; finally System.out.println(a+的平方值为:+a*a); 二、匿名类实验1运行下面的Test类,写出运行结果,并分析比较代码1与代码2两种方式的异同abstract class A abstract void abc(); void bbb()abc();class B exte
13、nds A void abc()System.out.println(bbb);class Test static void f(A a) a.bbb(); public static void main(String args) f( new A() void abc()System.out.println(bbb); ); /代码1 f(new B();/代码2 代码1和代码2的异同:代码1是通过匿名类来实现在代码块中对于A类的继承和对象的创建,此时这个创建好的对象就相当于static void f(A a) a.bbb();中参数值a。代码2则是直接通过在Test2类中的方法static
14、 void f(A a) a.bbb();中的参数部分直接创建议了一个A类的对象,然后实现了该方法的功能。2运行下面的Test类,写出运行结果,并分析比较代码1与代码2两种方式的异同interface IA void abc(); void bbb();class A implements IA public void bbb()System.out.println(ccc); public void abc()System.out.println(bbb);class Test static void f(IA a) a.bbb(); public static void main(String args)f(new IA() public void bbb()System.out.println(ccc); public void abc()System.out.println(bbb); ); /代码1f(new A();/代码2 代码1和代码2的异同在于:代码1是通过在方法块中利用匿名类来实现接口IA,并同时创建对象,该对象就成为f方法所需要传递进去的参数值;代码2是直接给f方法创建了一个对象作为参数值。实验报告要求将编程题源程序、运行结果,以及实验中遇到的问题和解决问题的方法,写在实验报告上。