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

来源:互联网 发布:mac能用淘宝千牛吗 编辑:程序博客网 时间:2024/05/16 02:49

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); 异常

原创粉丝点击