千锋逆战班第26天

1.填空

Java中所有的错误都继承自_Throwable_类 ;在该类的子类中,Error类表示严重的底层错误 ,对于这类错误一般处理的方式是__不能手动处理_ ;Exception 类 表示例外、异常。4
2.查询API ,填空
I.异常类 java.rmi.AlreadyBoundException,从分类上说,该类属于__已检查_ ( 已检查|运行时)异常,
从处理方式上说,对这种异常__try-catch捕获异常或者throws声明__处理。
II.异常类 java.util.regex.PatternSyntaxException ,从分类上说,该类属于_运行时__ ( 已检查|运行时)异常,从处理方式上说,对这种异常__try-catch捕获_处理。
3. (异常的产生)把下面代码补充完整

 public class TestThrow{ 
 public static void main(String args[]){ 
 throwException(10);
} public static void throwException(int n){
 if (n == 0){ //抛出一个 
 NullPointerException 
 }else{ 
 //抛出一个 ClassCastException //并设定详细信息为“类型转换出错” 
 } } }

补全代码:throw new NullPointerException;

throw new ClassCastException

System.out.println(“类型转换出错”);
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 时,输出的结果分别是什么?
输入1时:
main 1
ma1
mb1
Catch EOFException
In finally
main2
输入2时:
main 1
ma1
mb1
Catch lOException
In finally
main2
输入3时:
main 1
ma1
mb1
Catch SQL Exception
In finally
main2
输入4时
main 1
ma1
mb1
Catch Exception
In finally
main2
输入5时:
main 1
ma1
mb1
mb2
ma2
In finally
main2
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(); 
} 
}

MyException要继承Exception,然后m()方法要声明异常
9.(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
A. 编译不通过
B. 编译通过,输出-1
C. 编译通过,输出 0
11.(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 ma21 In Finally
如果读入的值为 0,则输出: ma1 InFinally
13.(异常的捕获和抛出)有以下代码

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”); 
  } 
  }

选择正确答案:C
A. 编译出错
B. 编译正常,输出 main1 ma1 In Catch
C. 编译正常,运行时出错
14.(异常的捕获和抛出)有如下代码

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

下面哪些代码放在/1/处可以编译通过? ABC
A. catch(NullPointerException npe){}
B. catch(IOException ioe){}
C. catch(SQLException sqle){}

发布了23 篇原创文章 · 获赞 0 · 访问量 1944

猜你喜欢

转载自blog.csdn.net/funager/article/details/104761350