java异常类匿名类实验报告.docx
- 文档编号:25938601
- 上传时间:2023-06-16
- 格式:DOCX
- 页数:15
- 大小:118.09KB
java异常类匿名类实验报告.docx
《java异常类匿名类实验报告.docx》由会员分享,可在线阅读,更多相关《java异常类匿名类实验报告.docx(15页珍藏版)》请在冰豆网上搜索。
java异常类匿名类实验报告
实验十四异常类、匿名类
课程名称:
面向对象程序设计
实验项目名称:
实验十四异常类、匿名类
【实验目的】
1.掌握Java异常类、嵌套类、匿名类。
【实验内容】
一、异常类实验
1.编译下面的程序,并回答问题。
classExceptionTest1{
publicstaticvoidmain(String[]args){
inta=0,b=0;
try{
a=Integer.parseInt(args[0]);//语句1
b=10;//语句2
}
catch(NumberFormatExceptione){
a=1;
}
System.out.println(a);
System.out.println(b);
}
}
(1)执行上述ExceptionTest1类(在命令提示符下敲入javaExceptionTest199)
运行结果:
(2)执行上述ExceptionTest1类(在命令提示符下敲入javaExceptionTest1a)
运行结果:
(3)比较上面两次运行的结果,说明原因。
(思考:
执行语句1时发生异常,语句2执行没有?
)
执行语句1时没有发生异常,语句2执行发生异常。
因为在源代码中写有
try{
a=Integer.parseInt(args[0]);//语句1
b=10;//语句2
}
catch(NumberFormatExceptione){
a=1;
}
当在运行代码时,若在文件名后面输入的是整数,则不会发生异常,try代码块的下面的代码继续执行,而当输入的是字符或是字符串时,try代码块会捕获错误的信息,但是并没有运行下面的代码,如果try代码块中添加了finally部分,将原来的System.out.println(a);
System.out.println(b);填入到finally中,则无论有没有发生异常,都能执行到finally中的代码。
2.编译下面的程序,并回答问题。
classExceptionTest2{
publicstaticvoidmain(String[]args){
inta=0;
try{
a=Integer.parseInt(args[0]);
}
}
}
(1)编译上述程序,是否出错?
出错
(2)如果出错,给出出错信息(即javac编译该文件出错信息):
分析为何出错?
出错原因:
try和catch或finally是成对出现的,不能出现了try,而后面就没有了catch或者finally
(3)如果将程序改写为:
classExceptionTest2{
publicstaticvoidmain(String[]args){
inta=0;
try{
a=Integer.parseInt(args[0]);
}
catch(NumberFormatExceptione){
a=1;
}
}
}
上述程序是否编译出错?
为什么?
上述程序中try与catch是成对出现的,所以编译不会出错。
(4)如果将程序改写为:
classExceptionTest2{
publicstaticvoidmain(String[]args){
inta=0;
try{
a=Integer.parseInt(args[0]);
}
finally{
a=1;
}
}
上述程序是否编译出错?
不出错
为什么?
当出现了try时,后面要么接catch,要么接finally,但是不能这两个都不接。
(4)如果将程序改写为:
classExceptionTest2{
publicstaticvoidmain(String[]args){
inta=0;
try{
a=Integer.parseInt(args[0]);
}
catch(Exceptione){
a=1;
}
catch(NumberFormatExceptione){
a=2;
}
}
}
上述程序是否编译出错?
分析为何出错,如何修改程序使之正确?
处理子类异常的catch代码块要写在前面,超类写在后面。
classExceptionTest2{
publicstaticvoidmain(String[]args){
inta=0;
try{
a=Integer.parseInt(args[0]);
}
catch(NumberFormatExceptione){
a=2;
}
catch(Exceptione){
a=1;
}
}
}
3、编译下面的程序,并回答问题。
classExceptionTest3{
publicstaticvoidmain(String[]args){
check(-1);
}
staticvoidcheck(inti){
if(i<0)thrownewException();
}
}
(1)上述程序编译是否出错?
出错
分析错误原因:
(2)将上述程序修改为:
classExceptionTest3{
publicstaticvoidmain(String[]args){
check(-1);
}
staticvoidcheck(inti)throwsException{
if(i<0)thrownewException();
}
}
那么,上述程序编译是否出错?
出错
分析出错原因:
4.编译下面程序,回答问题。
classExceptionTest4{
publicstaticvoidmain(String[]args){
method3();
System.out.println("success!
");
}
staticvoidmethod1(){
thrownewException();
}
staticvoidmethod2(){
method1();
}
staticvoidmethod3(){
method2();
}
}
(1)上述程序编译是否出错?
出错
编译错误是什么?
(2)修正上述程序(通过两种方式:
给方法添加throws子句,或者使用try-catch-finally)。
classExceptionTest4{
publicstaticvoidmain(String[]args){
method3();
System.out.println("success!
");
}
staticvoidmethod1(){
try{
thrownewException();
}catch(Exceptione){
//TODO自动生成catch块
e.printStackTrace();
}
}
staticvoidmethod2(){
method1();
}
staticvoidmethod3(){
method2();
}
}
(3)如果将方法method1中的thrownewException();更改为:
thrownewRuntimeException();
那么程序是否编译通过?
通过
如果编译正常,那么执行ExceptionTest4类,出现什么结果(是否会显示success字符串)?
如何修改程序使得能够显示success字符串?
classExceptionTest4{
publicstaticvoidmain(String[]args){
try{
method3();
}catch(RuntimeExceptione){
System.out.println("success!
");
}
}
staticvoidmethod1(){
thrownewRuntimeException();
}
staticvoidmethod2(){
method1();
}
staticvoidmethod3(){
method2();
}
}
5、设计一个简单的类ExceptionTest,在该类的main方法中实现:
将输入参数args[0]转换为int类型的数字(要求使用包装类的数字转换方法,并通过try-catch-finally方式捕获并处理转换异常),然后显示这个数字的平方值。
比如,在命令提示符下输入ExceptionTest9并回车,程序显示81。
classExceptionTest{
publicstaticvoidmain(String[]args){
inta=0;
try{
a=Integer.parseInt(args[0]);//语句1
}
catch(NumberFormatExceptione){
a=1;
}
finally{
System.out.println(a+"的平方值为:
"+a*a);
}
}
}
二、匿名类实验
1.运行下面的Test类,写出运行结果,并分析比较代码1与代码2两种方式的异同
abstractclassA{
abstractvoidabc();
voidbbb(){abc();}
}
classBextendsA{
voidabc(){System.out.println("bbb");}
}
classTest{
staticvoidf(Aa){a.bbb();}
publicstaticvoidmain(String[]args){
f(newA(){
voidabc(){System.out.println("bbb");}
}
);//代码1
f(newB());//代码2
}
}
代码1和代码2的异同:
代码1是通过匿名类来实现在代码块中对于A类的继承和对象的创建,此时这个创建好的对象就相当于staticvoidf(Aa){a.bbb();}中参数值a。
代码2则是直接通过在Test2类中的方法staticvoidf(Aa){a.bbb();}中的参数部分直接创建议了一个A类的对象,然后实现了该方法的功能。
2.运行下面的Test类,写出运行结果,并分析比较代码1与代码2两种方式的异同
interfaceIA{
voidabc();
voidbbb();
}
classAimplementsIA{
publicvoidbbb(){System.out.println("ccc");}
publicvoidabc(){System.out.println("bbb");}
}
classTest{
staticvoidf(IAa){a.bbb();}
publicstaticvoidmain(String[]args){
f(newIA(){
publicvoidbbb(){System.out.println("ccc");}
publicvoidabc(){System.out.println("bbb");}
}
);//代码1
f(newA());//代码2
}
}
代码1和代码2的异同在于:
代码1是通过在方法块中利用匿名类来实现接口IA,并同时创建对象,该对象就成为f方法所需要传递进去的参数值;代码2是直接给f方法创建了一个对象作为参数值。
实验报告要求
将编程题源程序、运行结果,以及实验中遇到的问题和解决问题的方法,写在实验报告上。
温馨提示:
最好仔细阅读后才下载使用,万分感谢!
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 异常 匿名 实验 报告