千锋Java&Day26课后作业

异常

1.填空

Java 中所有的错误都继承自__**Throwable**__类;
在该类的子类中,___**Error**___类表示严重的底层错误,
对于这类错误一般处理的方式是__**不处理**____;
__**Exception**____类表示例外、异常。

2.查询 API,填空

1) 异常类 java.rmi.AlreadyBoundException,从分类上说,该类属于___**已检查异常**___(已检查|运行时)异常,
从处理方式上说,对这种异常___**必须要处理**___处理。
2)异常类 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
           }else{
           //抛出一个 ClassCastException
           //并设定详细信息为“类型转换出错”
           }
      }
}
// 抛出一个 NullPointerException
            throw new NullPointerException();
// 抛出一个 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 分别为 12345 时,输出的结果分别是什么?
当读入的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
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();
      }
}
class MyException extends Exception{
	 public MyException(){	
	 	  super();		
	 }		
	  public MyException(String msg){	
	  	  super(msg);		
	 }
}
    public class Test1 {
    	public static void main(String[] args) {	
    	     ma();	
    	 }
     	public static int ma() {		
     	    try {		
     	        m();		
     	      	return 100;	
     	   	}catch(ArithmeticException e) {
     	   		System.out.println("ArithmeticException");	
     	   	}catch(Exception e) {	
     	   		System.out.println("Exception");	
     	   	}	
    }
    	public static void m() throws MyException {	
    		throw new MyException();	
    	}
  }

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.编译不通过
B.编译通过,输出-1
C.编译通过,输出 0
A
注:try中的代码块可能不会被执行

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,则输出: ______ 。
如果读入的值为 0,则输出: ______ 。
b=10:
ma1
ma2 1
In Finally

 b=0:
 ma1
 In Finally

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”);
          }
   }
   选择正确答案:
   A.编译出错
   B.编译正常,输出 main1 ma1 In Catch
   C.编译正常,运行时出错
A

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*/处可以编译通过?
    A.catch(NullPointerException npe){}
    B.catch(IOException ioe){}
    C.catch(SQLException sqle){}
AB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值