2.1 异常类java.rmi.AlreadyBoundException,从分类上说,该类属于______(已检查|运行时)异常,从处理方式上说,对这种异常______处理。
2.2 异常类java.util.regex.PatternSyntaxException,从分类上说,该类属于______(已检查|运行时)异常,从处理方式上说,对这种异常______处理。
public class TestThrow { public static void main(String args[]) { throwException(10); } public static void throwException(int n) { if (n == 0) { //抛出一个NullPointerException } else { //抛出一个ClassCastException //并设定详细信息为“类型转换出错” } } }
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"); } }
5.1 MyException1为已检查异常,MyException2为运行时异常。
5.2 这两个异常均具有两个构造函数,一个无参,另一个带字符串参数,参数表示产生异常的信息。
public class TestMyException { public static void main(String args[]){ int n; //读⼊n try{ m(n); }catch(MyException1 ex1){ //输出ex1 详细的⽅法调⽤栈信息 }catch(MyException2 ex2){ //输出ex2 的详细信息 //并把ex2 重新抛出 } } public static void m(int n)_________ { //声明抛出MyException1 if (n == 1) { //抛出MyException1 //并设定其详细信息为“n == 1” }else { //抛出MyException2 //并设定其详细信息为“n == 2” } } }
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(); } }
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______{} }
A. throws java.io.IOException
B. throws java.io.FileNotFoundException, java.io.EOFException
C. throws java.sql.SQLException
D. 不能抛出任何异常。
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
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; } } }
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"); } } }
import java.io.*; 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 { } }
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. 编译正常,运行时出错
A. catch(NullPointerException npe){}
B. catch(IOException ioe){}
C. catch(SQLException sqle){}
import java.io.*; import java.sql.*; class TestException { public static void main(String args[]) { try { ma(); } /* 1 */ catch (Exception e) { } } public static void ma() throws IOException { } }