收藏 分销(赏)

Java-异常(习题).doc

上传人:人****来 文档编号:3107297 上传时间:2024-06-18 格式:DOC 页数:11 大小:92.50KB
下载 相关 举报
Java-异常(习题).doc_第1页
第1页 / 共11页
Java-异常(习题).doc_第2页
第2页 / 共11页
Java-异常(习题).doc_第3页
第3页 / 共11页
Java-异常(习题).doc_第4页
第4页 / 共11页
Java-异常(习题).doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

1、java异常(习题)Key Point* 异常的概念和分类* 异常的产生和传递* 异常的处理* 自定义异常练习1. 填空Java 中所有的错误都继承自_类;在该类的子类中,_类表示严重的底层错误,对于这类错误一般处理的方式是_;_类表示例外、异常。2. 查api,填空异常类java.rmi.AlreadyBoundException,从分类上说,该类属于_(已检查|未检查)异常,从处理方式上说,对这种异常_;异常类java.util.regex.PatternSyntaxException,从分类上说,该类属于_(已检查|未检查)异常,从处理方式上说,对这种异常_。3. (异常的产生)把下面代

2、码补充完整package exception;public class TestThrow public static void main(String args) throwException(10);public static void throwException(int n) if (n = 0) / 抛出一个NullPointerException else / 抛出一个ClassCastException/ 并设定详细信息为“类型转换出错”4. (try-catch-finally)有如下代码:import java.io.*;import java.sql.*;class Tes

3、tException public static void main(String args) System.out.println(main 1);int n;/ 读入nma(n);System.out.println(main2);public static void ma(int n) try System.out.println(ma1);mb(n);System.out.println(ma2); catch (EOFException e) System.out.println(Catch EOFException); catch (IOException e) System.ou

4、t.println(Catch IOException); catch (SQLException e) System.out.println(Catch SQLException); catch (Exception e) System.out.println(Catch Exception); finally System.out.println(In finally);public static void mb(int n) throws Exception System.out.println(mb1);if (n = 1)throw new EOFException();if (n

5、= 2)throw new FileNotFoundException();if (n = 3)throw new SQLException();if (n = 4)throw new NullPointerException();System.out.println(mb2);问:当读入的n 分别为1,2,3,4,5 时,输出的结果分别是什么?5. (自定义异常)创建两个自定义异常类MyException1 和MyException2。要求:1) MyException1 为已检查异常,MyException2 为未检查异常2) 这两个异常均具有两个构造函数,一个无参,另一个带字符串参数,参

6、数表示产生异常的详细信息。6. (自定义异常)在上一题的基础上,把下面代码补充完整。public class TestMyException public static void main(String args) int n;/ 读入ntry m(n); catch (MyException1 ex1) / 输出ex1 详细的方法调用栈信息 catch (MyException2 ex2) / 输出ex2 的详细信息/ 并把ex2 重新抛出public static void m(int n) / 声明抛出MyException1if (n = 1) / 抛出MyException1/ 并设

7、定其详细信息为“n = 1” else / 抛出MyException2/ 并设定其详细信息为“n = 2”7. (try-catch)代码改错。class MyException class TestException public static void main(String args) ma();public static int ma() try m();return 100; catch (Exception e) System.out.println(Exception); catch (ArithmeticException e) System.out.println(Arit

8、hmeticException);public static void m() throw new MyException();8. (方法覆盖)有如下代码class Super public void ma() throws IOException interface IA void mb();class MySub extends Super implements IA public void ma()/ 1_public void mb()/ 2_问:在/1 处,填入以下_代码可以编译通过,在/2 处,填入_代码可以编译通过。A. throws java.io.IOExceptionB.

9、 throws java.io.FileNotFoundException, java.io.EOFExceptionC. throws java.sql.SQLExceptionD. 不能抛出任何异常。9. *(异常处理)有如下代码import java.io.*;import java.sql.*;public class TestTryCatch public static void main(String args) try ma(10);System.out.println(No Exception); catch (EOFException ex1) System.out.prin

10、tln(ex1); catch (IOException ex2) System.out.println(ex2); catch (SQLException ex3) System.out.println(ex3);public static void ma(int n) throws Exception if (n = 1) throw new IOException(); else if (n = 2) throw new EOFException(); else if (n = 3) throws new SQLException();选择正确答案:A 编译不通过B 编译通过,输出No

11、ExceptionC 编译通过,输出ex1D 编译通过,输出ex2E 编译通过,输出ex310. *(try-catch,局部变量)有如下代码public class TestTryCatch public static void main(String args) System.out.println(ma();public static int ma() int n;try n = 10 / 0; catch (Exception e) return n;选择正确答案:A. 编译不通过B. 编译通过,输出-1C. 编译通过,输出011. *(try-catch-finally)有如下代码p

12、ublic class TestFinally public static void main(String args) System.out.println(ma();public static int ma() int b=0;/ 读入btry int n = 100;return n / b; catch (Exception e) return 10; finally return 100;在ma 中,当读入的b 为100 时,输出结果为_,当读入的b 为0 时,输出结果为_。12. *(try-finally)写出下面代码运行的结果public class TestTryFinall

13、y public static void main(String args) try ma(); catch (Exception ex1) public static void ma() throws Exception int n = 10;int b;/ 读入一个整数btry System.out.println(ma1);int result = n / b;System.out.println(ma2 + result); finally System.out.println(In Finally);在ma 中,读入整数b,如果读入的值为10,则输出:如果读入的值为0,则输出:13.

14、 *(方法覆盖)class MySuper public void m() throws IOException class MySub extends MySuper public void m() throws EOFException class MySub2 extends MySub public void m() throws FileNotFoundException 以上代码是否能编译通过?如果不能,应该如何修改?14. *(自定义异常)完成某个计费系统的用户登录和注册模块,要求如下:1) 创建一个User 类,包括:用户登录名(username)、密码(password)、用

15、户真实姓名(name)、电子邮件地址(email)属性和相应的构造方法及set/get 方法。2) 创建两个自定义异常类,一个LoginException,表示登录异常。一个RegisterException,表示注册异常。自定义的两个异常,都要求有一个接受字符串类型参数的构造方法。3) 创建一个UserBiz 接口,该接口中定义两个方法:void register(String username, String password, String password2,String name, String email) throws RegisterException /用户注册void lo

16、gin(String username, String password) throws LoginException /用户登录其中register 方法接受两个password 参数,原因是:在用户注册时,需要输入两遍password,只有两次输入的password 一致,才允许注册。4) 创建UserBiz 接口的实现类。其中为该实现类创建一个属性,该属性为一个Map,用来保存已注册的用户信息。Map的键为用户登录名,值为登录名对应的User 对象。初始情况下,Map 中保存的对象为以下两个:用户名 密码 真实姓名 电子邮件/admin admin Administratoradmin/

17、tom cat tomcattomcatregister 方法在以下两种情况下抛出异常:1) username 在Map 中已存在2) 两次输入的password 不一致login 方法在以下两种情况下抛出异常:1) username 不存在2) username 和password 不匹配5) 创建一个UserView 接口,该接口中定义两个方法:void login();void register();6) 创建UserView 接口的实现类。该实现类的login 方法中,利用命令行,让用户输入用户名和密码,之后调用UserBiz中的login 方法。部分代码如下:void login()

18、System.out.println(“请输入用户名:”);String username = ;System.out.println(“请输入密码”);String password = ;/调用UserBiz 中的login 方法该类的register 方法采用类似的方法,让用户输入注册时需要的信息,然后调用UserBiz 中的register 方法。注意:1、 密码应该让用户输入两遍。2、 UserViewImpl 中应当有一个UserBiz 类型的属性7) 编写测试代码。类图如下:15. *(异常的捕获和抛出)有以下代码:import java.io.*;import java.sql

19、.*;public class TestMyException public static void main(String args) try System.out.println(main1);ma();System.out.println(main2); catch (Exception e) System.out.println(Catch Exception in main);System.out.println(e.getMessage();public static void ma() throws IOException try System.out.println(ma1);

20、mb();System.out.println(ma2); catch (SQLException e) System.out.println(Catch SQLException in ma);throw new IOException(e.getMessage(); catch (Exception e) System.out.println(Catch Exception in ma);System.out.println(e.getMessage();public static void mb() throws SQLException throw new SQLException(s

21、ql exception in mb);问:该程序输出结果是什么?16. *(异常的捕获和抛出)有以下代码public class TestException public static void main(String args) try System.out.println(main1);ma();System.out.println(main2); catch (Exception e) System.out.println(In Catch);public static void ma() System.out.println(ma1);throw new NullPointerExc

22、eption();System.out.println(ma2);选择正确答案:A. 编译出错B. 编译正常,输出main1 ma1 In CatchC. 编译正常,运行时出错17. *(异常的捕获和抛出)有如下代码class TestException public static void main(String args) try ma();catch (Exception e) public static void ma() throws IOException 下面哪些代码放在处可以编译通过?A. catch(NullPointerException npe)B. catch(IOException ioe)C. catch(SQLException sqle)

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服