Day26

学习的第26天。

 

总结:

异常:

                异常的产生

                                自动抛出异常:当程序在运行时遇到不符合规范的代码或结果时

                                手动抛出异常:throw new 异常类型(“实际参数”);

                                遇到异常:相当于遇到return,无条件结束当前方法

                异常的传递:按照方法的调用链

                                受查异常:throws 声明异常, 修饰在方法参数列表的后端

 

                异常的处理

                                e.printStackTrace(); 打印堆栈跟踪信息

                                e.getMessage(); 获取异常的原因

 

 

习题:

C11.1:  填空:

                JAVA中所有的错误都继承自  throwable  类;在该类的子类中,  Error  类表示严重的底层错误,对于这类错误一般的处理方式是: 抛出   ; Exception  类表示例外、异常。

 

C11.2:  查API填空:

                异常类 java.rmi.AlreadyBoundException,从分类上说,该类属于  已检查  异常,从处理方式上说,对这种异常  必须  处理。

                异常类 java.util.regex.PatternSyntaxException,从分类上说,该类属于  运行时  异常,从处理方式上说,这种异常  可以不  处理。

 

C11.3:  代码补完:

public class TestThrow{
    public static void main(String args[]){
        throwException(10);
    }

    public statci void throwException(int n){
        if(n == 0){
            //抛出一个 NullPointerException
            throw new NullPointerException;
        }else{
            //抛出一个 ClassCastException
            //并设定详细信息为“类型转换出错”
            throw new NullPointerException(“类型转换出错”);
        }
    }
}

              

C11.4:  有如下代码:

public 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 EOF");
		}catch(IOException e){
			System.out.println("catch IO");
		}catch(SQLException e){
			System.out.println("catch SQL");
		}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\mb2\catch EOF\ma2\In finally\main2

                2:main 1\ma1\mb1\catch Exception\In finally\main2

                3:main 1\ma1\mb1\catch SQL\In finally\main2

                4:                         catch  Exception

                5:main 1\ma1\mb1\mb2\ma2\In finally\main2

 

C11.7:  代码改错:

class MyException{}    //---继承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");
        }
        catch(ArithmeticException e){
            System.out.println("ArithmeticException");
        }
    }

    //根据MyException考虑是否声明抛出?
    public static void m(){
        throw new MyException();
    }


}

 

C11.9:  有如下代码:

public class TestTryCatch{
    public static void main(String[] args){
        System.out.println(ma());
    }

    public static int ma(){
        int n;
        try{
            n = 10/0;
        }catch(Excption e){}
        return n;
    }
}

                正确答案:A

                                A  编译不通过

                                B  编译通过,输出-1

                                C  编译通过,输出0

 

 

C11.11:  写出运行结果:

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.print("ma1");
			int result = n / b;
			System.out.println("ma2" + result);
		}finally{
			System.out.println("In Finally");
		}
	}
}

                在 ma 中,读入整数b,如果读入的值为10,则输出:  ma1\ma21\In Finally

                在 ma 中,读入整数b,如果读入的值为 0,则输出:  ma1\In Finally

 

C11.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

 

C11.14:  有如下代码:

class TestException{
    public static void main(String[] args){
        try{
            ma();
        }
        /*1*/
        catch(Exception e){}
    }

    public static void ma() throws IOException{}
}

                哪些放在1处可通过   AB

                A  catch(NullPointerException npe){}

                B  catch(IOException npe){}

                C  catch(SQLException npe){}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值