当前位置:首页 > Java 异常(习题)
public class TestTryCatch { public static void main(String args[]) { try { ma(10); System.out.println(\ } catch (EOFException ex1) { System.out.println(\ } catch (IOException ex2) { System.out.println(\ } catch (SQLException ex3) { System.out.println(\ } } 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 Exception C. 编译通过,输出ex1 D. 编译通过,输出ex2 E. 编译通过,输出ex3
10. *(try-catch,局部变量)有如下代码 public class TestTryCatch { public static int ma() { int n; try { n = 10 / 0; public static void main(String args[]) { } System.out.println(ma()); } } } catch (Exception e) { } return n; 选择正确答案: A. 编译不通过
B. 编译通过,输出-1 C. 编译通过,输出0
11. *(try-catch-finally)有如下代码 public class TestFinally { } public static int ma() { } int b=0; // 读入b try { } int n = 100; return n / b; return 10; return 100; public static void main(String args[]) { } System.out.println(ma()); } catch (Exception e) { } finally {
在ma 中,当读入的b 为100 时,输出结果为____,当读入的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.out.println(\); int result = n / b; System.out.println(\ + result); System.out.println(\); } finally {
在ma 中,读入整数b,如果读入的值为10,则输出: 如果读入的值为0,则输出: 13. *(方法覆盖) class MySuper { } class MySub extends MySuper { } class MySub2 extends MySub { } public void m() throws FileNotFoundException { } public void m() throws EOFException { } public void m() throws IOException { } 以上代码是否能编译通过?如果不能,应该如何修改?
14. *(自定义异常)完成某个计费系统的用户登录和注册模块,要求如下: 1) 创建一个User 类,包括:用户登录名(username)、密码(password)、用户真实姓 名(name)、电子邮件地址(email)属性和相应的构造方法及set/get 方法。
2) 创建两个自定义异常类,一个LoginException,表示登录异常。一个RegisterException, 表示注册异常。自定义的两个异常,都要求有一个接受字符串类型参数的构造方法。 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,只有两次输入的password 一致,才允许注册。 4) 创建UserBiz 接口的实现类。其中
为该实现类创建一个属性,该属性为一个Map,用来保存已注册的用户信息。Map 的键为用户登录名,值为登录名对应的User 对象。初始情况下,Map 中保存的对 象为以下两个:
用户名 密码 真实姓名 电子邮件
//admin admin Administrator admin@123.com //tom cat tomcat tomcat@cat.com
register 方法在以下两种情况下抛出异常: 1) username 在Map 中已存在 2) 两次输入的password 不一致
login 方法在以下两种情况下抛出异常: 1) username 不存在
2) 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 方法 }
该类的register 方法采用类似的方法,让用户输入注册时需要的信息,然后调用 UserBiz 中的register 方法。 注意:
1、 密码应该让用户输入两遍。
2、 UserViewImpl 中应当有一个UserBiz 类型的属性 7) 编写测试代码。 类图如下:
共分享92篇相关文档