Java基础(九)异常处理和断言

9-1 用于异常处理的关键字有哪些?
throw
throws
try
catch
finally

9-2 查找帮助文档,画出下列类的继承关系图。
java.lang.Throwable、java.lang.Error、java.lang.Exception、java.lang.OutOfMemory、java.io.IOException、java.io.StreamCorruptedException、java.net.MalformedURLException

这里写图片描述

9-3 根据上一题的继承关系图,如果下列程序第5行出现一个MalformedURLException类型的异常,那么输出结果有:

try {
     //assume s is previously desfined
     URL u=new URL(s);
     // in is an ObjectInputStream
     Object o=in.readObject();
     System.out.println("Success");

catch(MalformedURLException e ){

     System.out.println("Bad URL");
}

catch(StreamCorruptedException e){
     System.out.println("Bad file contents");
}
catch (Exception e){
     System.out.println("General exception");

}

finally{
     System.out.println("Doing finally part");
}

输出结果为:
Bad URL
Doing finally part

9-4 如果第三行出现MalformedURLException异常,程序的输出是:

try 
//s 是已定义的字符串
URL u=new URL(s);
//in 是一个objectInputStream流
Object o=in.readObject("Success");
}
catch(MalformedURLException e){
System.out.println("Bad URL");
}
catch(StreamCorruptedException e){
System.out.println("Bad file contents");

}

catch(Exception e){
System.out.println("General exception");

}
finally{
System.out.println(doing finally part);
}
System.out.println("Carrying on");

输出结果为:
Bad URL
diing finally part
Carrying on

9-5 如果第三行和第五行都没有异常。程序的输出是什么?

try{
//s是已定义的字符串
URL u=new URL(s);
//in是一个ObjectInputStream流
Object o=in.readObject();
System.out.println("Success");


}
catch(MalformedURLException e){
System.out.println("Bad URL");
}
catch(StreamCorruptedException e){
System.out.println("Bad life contents");

}
catch(Exception e){
System.out.println("General exception");
}
finally{
System.out.println("Doing finally part");

}
System.out.println("Carring on");

输出结果为:
Success
Doing finally part
Carrying on

9-6 下列那一个是出现异常的正确方式?

throws new IOException();
throws IOException;
throws IOException();
throw new IOException();

正确的是:
throw new IOException();

9-7 假定启用了断言,下列哪些断言语句会出现异常?

assert true:true;
assert true:false;
assert false:true;

assert false:true;将出现异常

9-8 如何设置才可以忽略断言语句?
将断言语句关闭
java-da 关闭所有用户类的assertion
java-dsa 关闭所有系统类的assertion
9-9 断言与C语言中的条件编译有什么区别?

9-10 对于方法:

int act(int value){
assert value>6:value<10;
return 10/value;
}

假定启用了断言,下面哪些语句将出现异常
act(4); 异常
act(6); 异常
act(10);
act(0); 异常

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值