java基础篇 之 异常丢失

博客指出代码执行中存在异常丢失的严重缺陷,如一个重要异常丢失而抛出普通异常,还提到在finally块直接返回函数会使异常无输出。针对这些情况,建议通过多加日志的方式,在出现错误处自行打印日志来应对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们看如下代码:

@Slf4j
public class Test {
    public static void main(String[] args) {
        try {
            try {
                test();
            } finally {
                test2();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public static void test() {
        System.out.println("test方法执行了");
        throw new RuntimeException("veryImportant exception");
    }

    public static void test2() {
        System.out.println("test2");
        throw new CustomeException("common exception");
    }
}

执行结果如下:


我们可以看到,在执行的适合,一个veryImportant exception丢失了,而抛出了一个common exception,这是相当严重的缺陷,因为异常可能会以一种比前面例子所示更微妙和难以察觉的方式完全丢失。相比之下,C++把“前一个异常还没处理就抛出下一个异常”的情形看成是糟糕的错误。

一种更加糟糕的编程手法如下所示:

package com.study.spring.transaction.controller;

import com.study.spring.transaction.CustomeException;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: dmz
 * @Description:
 * @Date: Create in 2:40 2019/4/21
 */
@Slf4j
public class Test {
    public static void main(String[] args) {
        try {
            try {
                test();
            } finally {
                return;
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public static void test() {
        System.out.println("test方法执行了");
        throw new RuntimeException("veryImportant exception");
    }

    public static void test2() {
        System.out.println("test2");
        throw new CustomeException("common exception");
    }
}

可以看到,在finally快中,我们直接将函数返回了,这个适合执行代码,会发现,即使抛出了异常,也不会有任何输出。

这种情况下我们怎么办呢?我觉得主要就是多加日志,虽然程序不会主动输出什么,但是我们可以在出现错误的地方自己打印日志

@Slf4j
public class Test {
    public static void main(String[] args) {
        try {
            try {
                test2();
            } finally {
                return;
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public static void test() {
        System.out.println("test方法执行了");
        throw new RuntimeException("veryImportant exception");
    }

    public static void test2() {
        System.out.println("test2");
        CustomeException common_exception = new CustomeException("common exception");
        log.error("test2调用失败", common_exception);
        throw common_exception;
    }

调用结果:

test2
00:46:09.508 [main] ERROR com.study.spring.transaction.controller.Test - test2调用失败
com.study.spring.transaction.CustomeException: common exception
	at com.study.spring.transaction.controller.Test.test2(Test.java:33)
	at com.study.spring.transaction.controller.Test.main(Test.java:16)

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值