java 异常练习题_Java 异常(习题)

异常

Key Point

* 异常的概念和分类

* 异常的产生和传递

* 异常的处理

* 自定义异常

练习

1. 填空

Java 中所有的错误都继承自throwable类;在该类的子类中,

Error类表示严重的底层错误,对于这类错误一般处理的方式是不要求我们对其处理

Exception类表示例外、异常。

2. 查api,填空

异常类java.rmi.AlreadyBoundException,从分类上说,该类属于已检查(已检查|

未检查)异常,从处理方式上说,对这种异常 必须处理,进行声明抛出或异常捕获;

异常类java.util.regex.PatternSyntaxException,从分类上说,该类属于未检查(已检

查|未检查)异常,从处理方式上说,对这种异常不作处理。

3. (异常的产生)把下面代码补充完整

public class TestThrow{

public static void main(String args[]){

throwException(10);

}

public static void throwException(int n){

if (n == 0){

//抛出一个NullPointerException

throw new

NullPointerException();}else{

//抛出一个ClassCastException

//并设定详细信息为“类型转换出错”

throw new

ClassCastException("类型转换出错");}

}

}

4. (try-catch-finally)有如下代码:

import 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 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.println(“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 分别为1,2,3,4,5 时,输出的结果分别是什么?

结果: n = 1

main 1

ma1

mb1

Catch EOFException

In finally

main2

n = 2

main 1

ma1

mb1

Catch IOException

In finally

main2

n = 3

main 1

ma1

mb1

Catch SQLException

In finally

main2

n = 4

main 1

ma1

mb1

Catch Exception

In finally

main2

n = 5

main 1

ma1

mb1

mb2

ma2

In finally

main25. (自定义异常)创建两个自定义异常类MyException1

和MyException2。

要求:

1) MyException1 为已检查异常,MyException2 为未检查异常

2) 这两个异常均具有两个构造函数,一个无参,另一个带字符串参数,参数表示产生

异常的详细信息。

public class MyException1 extends

Exception{

public MyException1() {

}

public

String MyException1(String str) {

return str;

}

}

public class MyException2

extends RuntimeException{

public MyException2() {

}

public String MyException2(String str) {

return str;

}

}6.

(自定义异常)在上一题的基础上,把下面代码补充完整。

public class TestMyException{

static MyException1 me = new

MyException1();public static void main(String

args[]){

int n;

//读入n

try{

m(n);

}catch(MyException1 ex1){

//输出ex1 详细的方法调用栈信息

System.out.println(me.getStackTrace1());

}catch(MyException2 ex2){

//输出ex2 的详细信息

//并把ex2 重新抛出

String str = "n == 2";

System.out.println(ex2.MyException2(str));

throw new

MyException2();}

}

public static void m(int n)_________ { //声明抛出MyException1

if (n == 1) {

//抛出MyException1

//并设定其详细信息为“n == 1”

String str = "n == 1";

me.setStackTrace(str);

throw new

MyException1();

}else {

//抛出MyException2

//并设定其详细信息为“n == 2”

throw new

MyException2();}

}

}

7. (try-catch)代码改错。

class MyException extends

Exception{}

class TestException{

public static void main(String args[]){

ma();

}

public static int ma(){

try{

m();

return 100;

}catch(Exception e){

System.out.println(“Exception”);

return 0;}

//catch(ArithmeticException e){

//System.out.println(“ArithmeticException”);

//}}

public static void m()throws MyException{

throw new MyException();

}

}

8. (方法覆盖)有如下代码

import java.io.IOException;

class Super{

public void ma() throws IOException{}

}

interface IA{

void mb();

}

public class MySub extends Super implements IA{

public void ma() //1_________{

}

public void mb() //2_________{

}

}

问:

在//1 处,填入以下________代码可以编译通过,在//2 处,填入_________代码可以编译

通过。

A. throws java.io.IOException

B. throws java.io.FileNotFoundException, java.io.EOFException

C. throws java.sql.SQLException

D. 不能抛出任何异常。

9. *(异常处理)有如下代码

import java.io.*;

import java.sql.*;

public class TestTryCatch{

public static void main(String args[])throws

Exception{

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();

}else if (n == 2){

throw new EOFException();

}else if (n == 3) {

throw new SQLException();

}

}

}

选择正确答案:

A. 编译不通过

B. 编译通过,输出No Exception

C. 编译通过,输出ex1

D. 编译通过,输出ex2

E. 编译通过,输出ex3

结果:A

10. *(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. 编译通过,输出-1

C. 编译通过,输出0

结果:A.

11. *(try-catch-finally)有如下代码

public class TestFinally{

public static void main(String args[]){

System.out.println ( ma() );

}

public static int ma(){

int b;

//读入b

try{

int n = 100;

return n/b;

}catch(Exception e){

return 10;

}finally{

return 100;

}

}

}

在ma 中,当读入的b 为100 时,输出结果为__100__,当读入的b 为0 时,输出结果为

__100___。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(“ma1”);

int result = n / b;

System.out.println(“ma2 ” + result);

}finally{

System.out.println(“In Finally”);

}

}

}

在ma 中,读入整数b,如果读入的值为10,则输出:

ma1

ma2 =1

In Finally如果读入的值为0,则输出:

ma1

In Finally

13. *(方法覆盖)

import java.io.*;

class MySuper{

public void m() throws IOException{}

}

class MySub extends MySuper{

public void m() throws EOFException, FileNotFoundException{}

}

class MySub2 extends MySub {

public void m() throws FileNotFoundException{}

}

以上代码是否能编译通过?如果不能,应该如何修改?

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) 编写测试代码。

类图如下:

a4c26d1e5885305701be709a3d33442f.png

15. **(异常的捕获和抛出)有以下代码:

import java.io.*;

import java.sql.*;

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");

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("sql 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 NullPointerException();

System.out.println(“ma2”);

}

}

选择正确答案:

A. 编译出错

B. 编译正常,输出main1 ma1 In Catch

C. 编译正常,运行时出错

17. **(异常的捕获和抛出)有如下代码

import java.io.*;

import java.sql.*;

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){}

18. **(finally)有如下代码

public class TestTryAndTry {

public static void main(String args[]){

System.out.println(ma());

}

public static int ma(){

try{

return 100;

}finally{

try{

return 200;

}finally{

return 500;

}

return 1000;

}

}

}

选择正确答案:

A. 编译错误

B. 输出100

C. 输出200

D. 输出500

E. 输出1000

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些 Java 接口练习题,供您练习和巩固相关知识。以下是一些题目: 1. 定义一个接口 Animal,包含方法 eat() 和 sleep(),同时定义一个实现该接口的类 Dog 和 Cat。要求 Dog 和 Cat 实现 eat() 和 sleep() 方法,其中 eat() 方法输出 "I am a dog/cat, and I am eating",sleep() 方法输出 "I am a dog/cat, and I am sleeping"。 2. 定义一个接口 Shape,包含方法 getArea() 和 getPerimeter(),同时定义一个实现该接口的类 Rectangle 和 Circle。要求 Rectangle 和 Circle 实现 getArea() 和 getPerimeter() 方法,其中 getArea() 方法返回形状的面积,getPerimeter() 方法返回形状的周长。 3. 定义一个接口 Calculator,包含方法 add()、subtract()、multiply() 和 divide(),同时定义一个实现该接口的类 BasicCalculator 和 ScientificCalculator。要求 BasicCalculator 和 ScientificCalculator 实现 add()、subtract()、multiply() 和 divide() 方法,其中 BasicCalculator 实现的方法为简单的四则运算,ScientificCalculator 实现的方法为科学计算器常用的函数,例如 sin、cos、tan 等。 4. 定义一个接口 Sortable,包含方法 sort(),同时定义一个实现该接口的类 BubbleSort 和 QuickSort。要求 BubbleSort 和 QuickSort 实现 sort() 方法,其中 BubbleSort 实现冒泡排序,QuickSort 实现快速排序。 希望这些题目能够帮助您巩固 Java 接口的相关知识。如果您有任何问题或需要更多帮助,请随时告诉我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值