ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:478.95KB ,
资源ID:1744931      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-1744931.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java异常习题共11页.docx)为本站会员(b****1)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

Java异常习题共11页.docx

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

2、异常,从处理方式上说,对这种异常_。3. (异常的产生)把下面代码补充完整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)有如下(rxi)代码:impo

3、rt java.io.*;import java.sql.*;class TestException public static void main(String args) System.out.println(main 1); int n; / 读入n ma(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

4、 EOFException); catch (IOException e) System.out.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.printl

5、n(mb1); if (n = 1) throw new EOFException(); if (n = 2) throw new FileNotFoundException(); if (n = 3) throw new SQLException(); if (n = 4) throw new NullPointerException(); System.out.println(mb2); 问:当读入的n 分别(fnbi)为1,2,3,4,5 时,输出的结果分别是什么?5. (自定义异常)创建两个(lin )自定义异常类MyException1 和MyException2。要求:1) MyE

6、xception1 为已检查异常,MyException2 为未检查异常2) 这两个异常均具有两个构造函数,一个无参,另一个带字符串参数,参数表示产生异常的详细信息。6. (自定义异常(ychng))在上一题的基础上,把下面代码补充完整。public class TestMyException public static void main(String args) int n; / 读入n try m(n); catch (MyException1 ex1) / 输出ex1 详细的方法调用栈信息 catch (MyException2 ex2) / 输出ex2 的详细信息 / 并把ex2 重

7、新抛出 public static void m(int n) / 声明抛出MyException1 if (n = 1) / 抛出MyException1 / 并设定其详细信息为“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

8、e) System.out.println(Exception); catch (ArithmeticException e) System.out.println(ArithmeticException); public static void m() throw new MyException(); 8. (方法覆盖)有如下(rxi)代码class Super public void ma() throws IOException interface IA void mb();class MySub extends Super implements IA public void ma()/

9、 1_ public void mb()/ 2_ 问:在/1 处,填入以下_代码可以编译通过,在/2 处,填入_代码可以编译通过。A. throws java.io.IOExceptionB. throws java.io.FileNotFoundException, java.io.EOFExceptionC. throws java.sql.SQLExceptionD. 不能抛出任何(rnh)异常。9. *(异常处理)有如下(rxi)代码import java.io.*;import java.sql.*;public class TestTryCatch public static vo

10、id main(String args) try ma(10); System.out.println(No Exception); catch (EOFException ex1) System.out.println(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(); el

11、se if (n = 2) throw new EOFException(); else if (n = 3) throws new SQLException(); 选择(xunz)正确答案:A 编译不通过B 编译通过,输出No ExceptionC 编译通过,输出ex1D 编译通过,输出ex2E 编译通过,输出ex310. *(try-catch,局部变量)有如下(rxi)代码public class TestTryCatch public static void main(String args) System.out.println(ma(); public static int ma(

12、) int n; try n = 10 / 0; catch (Exception e) return n; 选择正确答案:A. 编译不通过(tnggu)B. 编译通过,输出-1C. 编译通过,输出011. *(try-catch-finally)有如下(rxi)代码public class TestFinally public static void main(String args) System.out.println(ma(); public static int ma() int b=0; / 读入b try int n = 100; return n / b; catch (Exc

13、eption e) return 10; finally return 100; 在ma 中,当读入的b 为100 时,输出结果(ji gu)为_,当读入的b 为0 时,输出结果为_。12. *(try-finally)写出下面代码运行的结果public class TestTryFinally public static void main(String args) try ma(); catch (Exception ex1) public static void ma() throws Exception int n = 10; int b; / 读入一个整数b try System.o

14、ut.println(ma1); int result = n / b; System.out.println(ma2 + result); finally System.out.println(In Finally); 在ma 中,读入整数b,如果(rgu)读入的值为10,则输出:如果读入的值为0,则输出:13. *(方法覆盖)class MySuper public void m() throws IOException class MySub extends MySuper public void m() throws EOFException class MySub2 extends

15、MySub public void m() throws FileNotFoundException 以上代码是否能编译通过?如果不能,应该(ynggi)如何修改?14. *(自定义异常)完成某个(mu )计费系统的用户登录和注册模块,要求如下:1) 创建一个User 类,包括:用户登录名(username)、密码(password)、用户真实姓名(name)、电子邮件地址(email)属性和相应的构造方法及set/get 方法。2) 创建两个自定义异常类,一个LoginException,表示登录异常。一个RegisterException,表示注册异常。自定义的两个异常,都要求有一个接受字

16、符串类型参数的构造方法。3) 创建一个UserBiz 接口,该接口中定义两个方法:void register(String username, String password, String password2,String name, String email) throws RegisterException /用户注册void login(String username, String password) throws LoginException /用户登录其中register 方法接受两个password 参数,原因是:在用户注册时,需要输入两遍password,只有两次输入的pas

17、sword 一致,才允许注册。4) 创建UserBiz 接口的实现(shxin)类。其中为该实现类创建一个属性,该属性为一个Map,用来保存已注册的用户信息。Map的键为用户登录名,值为登录名对应的User 对象。初始情况下,Map 中保存的对象为以下两个:用户名 密码 真实姓名 电子邮件/admin admin Administratoradmin/tom cat tomcattomcatregister 方法在以下两种情况下抛出异常:1) username 在Map 中已存在2) 两次输入的password 不一致login 方法在以下两种情况下抛出异常:1) username 不存在2)

18、 username 和password 不匹配5) 创建一个UserView 接口,该接口中定义两个方法:void login();void register();6) 创建UserView 接口的实现类。该实现类的login 方法中,利用命令行,让用户输入用户名和密码,之后调用UserBiz中的login 方法。部分代码如下:void login()System.out.println(“请输入用户名:”);String username = ;System.out.println(“请输入密码”);String password = ;/调用UserBiz 中的login 方法该类的reg

19、ister 方法采用类似的方法,让用户输入注册时需要的信息,然后调用UserBiz 中的register 方法。注意:1、 密码应该让用户输入两遍。2、 UserViewImpl 中应当(yngdng)有一个UserBiz 类型的属性7) 编写测试代码。类图如下:15. *(异常(ychng)的捕获和抛出)有以下代码:import java.io.*;import java.sql.*;public class TestMyException public static void main(String args) try System.out.println(main1); ma(); Sy

20、stem.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); mb(); System.out.println(ma2); catch (SQLException e) System.out.println(Catch SQLException in ma); thr

21、ow 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(sql exception in mb); 问:该程序输出结果(ji gu)是什么?16. *(异常的捕获和抛出)有以下代码public class TestException public stat

22、ic 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 NullPointerException(); System.out.println(ma2); 选择正确答案:A. 编译出错B. 编译正常,输出main1 ma1 In CatchC. 编译正常,运行

23、(ynxng)时出错17. *(异常的捕获和抛出)有如下代码class TestException public static void main(String args) try ma(); catch (Exception e) public static void ma() throws IOException 下面哪些(nxi)代码放在处可以编译通过?A. catch(NullPointerException npe)B. catch(IOException ioe)C. catch(SQLException sqle)内容摘要(1)java异常(习题)练习1. 填空Java 中所有的错误都继承自_类(2)异常类java.util.regex.PatternSyntaxException,从分类上说,该类属于_(已检查|未检查)异常,从处理方式上说,对这种异常_(3)其中为该实现类创建一个属性,该属性为一个Map,用来保存已注册的用户信息(4)部分代码如下:void login()System.out.println(“请输入用户名:”)(5)类图如下:15. *(异常的捕获和抛出)有以下代码:问:该程序输出结果是什么

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2