资源预览内容
第1页 / 共11页
第2页 / 共11页
第3页 / 共11页
第4页 / 共11页
第5页 / 共11页
第6页 / 共11页
第7页 / 共11页
第8页 / 共11页
第9页 / 共11页
第10页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
java异常(习题)Key Point* 异常的概念和分类* 异常的产生和传递* 异常的处理* 自定义异常练习1. 填空Java 中所有的错误都继承自_类;在该类的子类中,_类表示严重的底层错误,对于这类错误一般处理的方式是_;_类表示例外、异常。2. 查api,填空异常类java.rmi.AlreadyBoundException,从分类上说,该类属于_(已检查|未检查)异常,从处理方式上说,对这种异常_;异常类java.util.regex.PatternSyntaxException,从分类上说,该类属于_(已检查|未检查)异常,从处理方式上说,对这种异常_。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)有如下代码:import java.io.*;import java.sql.*;class TestException 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.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 时,输出的结果分别是什么?5. (自定义异常)创建两个自定义异常类MyException1 和MyException2。要求:1) MyException1 为已检查异常,MyException2 为未检查异常2) 这两个异常均具有两个构造函数,一个无参,另一个带字符串参数,参数表示产生异常的详细信息。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/ 并设定其详细信息为“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(ArithmeticException);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. 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.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) throws new SQLException();选择正确答案:A 编译不通过B 编译通过,输出No 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)有如下代码public 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 TestTryFinally 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 + res
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号