Java核心技术 卷I 第七章

本文详细介绍了Java中的异常处理机制,包括异常的分类(Error和Exception,受查异常与非受查异常),异常的声明、抛出、捕获和再次抛出,异常链的使用。此外,还讲解了finally子句的应用,如资源的自动关闭,并演示了带资源的try语句。最后,讨论了断言assert的使用及其在IDEA中的开启方法,用于在开发阶段进行条件检查。
摘要由CSDN通过智能技术生成

Java核心技术 卷I 第七章



思维导图

在这里插入图片描述

7.1 异常分类

请添加图片描述

1)系统错误(Error):描述了Java运行时系统内部错误和资源耗尽错误
2)异常(Exception):程序和外部环境引起的错误,能被程序捕获和处理
3)运行时异常(RuntimeException):程序的设计错误——编程错误

派生于Error和RuntimeException的所有异常都是 [unchecked]/[非受查异常]/[免检异常]
其他异常都称为 [checked]/[受查异常]/[必检异常]

编译器为受查异常提供了异常处理器,强制需要try-catch或者方法头抛出

声明受查异常

throws 多个异常用逗号隔开

public FileInputStream(String name) throws FileNotFoundException{
}

抛出异常

EOFException异常:在输入过程中,遇到一个未预期的EOF后的信号

throw new EOFException();

捕获异常

不允许子类的throws出现超过超类方法所列出的异常
子类不能抛出范围更大的异常
超类没有抛出异常,子类必须自己处理

*捕获多个异常

try{
	//code that might throw exception
}catch(FileNotFoundException e){
	//emergency action for missing files
}catch(UnknowHostException e){
	//emergency action for unknow hosts
}catch(IOException e){
	//emergency action for all other I/O problems
}

*得到详细的错误信息

//获取异常有参构造的(String message)
e.getMessage();

*得到异常的实际对象类型

e.getClass().getName();
  • 异常的捕获顺序:子类优先
  • 多异常捕获合并:不允许是子类关系
  • 捕获多异常时,异常变量隐含为final,不能在子句中给e赋不同的值
try{
	System.out.println("xxxx");
}catch(RuntimeException|Exception e){//RuntimeException 是 Exception 的子类,编译报错
	//不能在这里给e赋其他值
	System.out.println("错误的多异常捕获");
}

受检异常捕获与抛出

  • 只有在try中必可能出现的受检异常,才可以在下面写类似IOException捕获
    下面的代码报错:因为无中生有
try{
	System.out.println("我不会抛出受检异常");
}catch(IOException e){
	System.out.println("try无受检异常,无中生有报错");
}
  • 这个可以
public class CatchException {
    public static void main(String[] args) throws IOException {
        System.out.println("我不会抛出受检异常");
    }
}

再次抛出异常与异常链

public static void main(String[] args) throws Throwable {
        try{
            throw new SQLException();
        }
        catch(SQLException e){
            Throwable se = new ServletException("data");
            se.initCause(e);
            throw se;
        }
    }

当捕获到异常时,Throwble e = se.getCause();得到原始异常

7.3 finally子句

try{
	//1
	code that might throw exceptions
	//2
}catch(IOException e){
    //3
    show error message
    //4
}finally {
    //5
}
//6

1)try没有异常——>1 2 5 6
2)try中出异常被捕获,在catch中没有抛出异常——>1 3 4 5 6
try中出异常被捕获,在catch中抛出异常——>1 3 5
3)try出异常,没被catch捕获——>1 5

解耦合的try/catch 和try/finally

InputStream in = ...;
try {
	try{
		code that might throw exceptions
	}finally {
        in.close();        
	}
} catch (IOException e) {
	e.printStackTrace();
}
  • 当finally中包含return?
    finally中的return会覆盖try中的return值
public static void main(String[] args){
        System.out.println(ret(1));//ret = 2
    }
    public static int ret(int n){
        try{
            return n;
            //不可达,在这不能写语句
        }finally {
            return n+1;
            //不可达,在这不能写语句
        }
    }

带资源的try语句

try-with-resources
try语块结束自动调用res.close();
catch子句和finally子句会在关闭资源后执行

try(Resource res = ...){
	//work with res
}
  • 一般的try语句必须有catch子句或者finally子句
  • try-with-resource可独立存在

7.4 断言assert

idea开启断言

在这里插入图片描述
在这里插入图片描述
断言抛异常 java.lang.AssertionError

public static void main(String[] args){
        int i = -1;

        if (i % 3 == 0) {
            System.out.println(0);
        } else if (i % 3 == 1) {
            System.out.println(1);

        } else {
            assert i % 3 == 2;//这里断言表达式不符合,抛异常
            System.out.println(i%3);
        }
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值