java异常丢失

Java异常

对于Java程序员而言相信大家都应该知道编译时异常、运行时异常,并对异常的使用应该都是驾轻就熟了,在这里就不过多的赘述。本篇文章主要是分析我们平常在使用异常时容易忽略的点。

异常吞噬

异常被吞噬主要是被catch吞噬和被finally吞噬。

  1. catch吞噬

    public static int x = 0;

    public static void main(String[] args) {
        System.out.println("main 结果:" + test1());
    }

    public static int test1() {
        try {
            System.out.println("test1 - " + ++x);
            test2();
            System.out.println("test1 - " + ++x);
            return x;
        } catch (Exception ex) {
            System.out.println("test1 - catch : " + ++x);
            throw new RuntimeException("test1 catch exception");
        }
    }

    public static void test2() {
        System.out.println("test2 - " + ++x);
        throw new RuntimeException("test2 exception");
    }
test1 - 1
test2 - 2
test1 - catch : 3
Exception in thread "main" java.lang.RuntimeException: test1 catch exception
	at com.bootcode.test.exceptions.TryCatchTest.test1(TryCatchTest.java:26)
	at com.bootcode.test.exceptions.TryCatchTest.main(TryCatchTest.java:15)

这里可以看出test2中的异常被test1的catch处理,但是在catch中我们抛出了其他的,并未对test2中的异常做处理,导致异常丢失。

  1. finally吞噬
    public static int x = 0;

    public static void main(String[] args) {
        System.out.println("main 结果:" + test1());
    }

    public static int test1() {
        try {
            System.out.println("test1 - " + ++x);
            test2();
            System.out.println("test1 - " + ++x);
            return x;
        } catch (Exception ex) {
            System.out.println("test1 - catch : " + ++x);
            throw new RuntimeException("test1 catch exception");
        } finally {
            throw new RuntimeException("test1 finally exception");
        }
    }

    public static void test2() {
        System.out.println("test2 - " + ++x);
        throw new RuntimeException("test2 exception");
    }
test1 - 1
test2 - 2
test1 - catch : 3
Exception in thread "main" java.lang.RuntimeException: test1 finally exception
	at com.bootcode.test.exceptions.TryCatchTest.test1(TryCatchTest.java:28)
	at com.bootcode.test.exceptions.TryCatchTest.main(TryCatchTest.java:15)

该实例中所有的一场都被finally截胡了,导致上面的异常都被吞噬了。这种情况会在资源关闭失败的时候出现。

finally打劫

我们在try、catch、finally中都return最终返回的是什么?

  1. 实例一

    public static int x = 0;

    public static void main(String[] args) {
        System.out.println("main 结果:" + test1());
    }

    public static int test1() {
        try {
            System.out.println("test1 - " + ++x);
            return x;
        } finally {
            System.out.println("test1 - finally : " + ++x);
            return x + 100;
        }
    }
test1 - 1
test1 - finally : 2
main 结果:102```

  1. 实例二
  public static int x = 0;

    public static void main(String[] args) {
        System.out.println("main 结果:" + test1());
    }

    public static int test1() {
        try {
            System.out.println("test1 - " + ++x);
            throw new RuntimeException("test1 - exception");
        } catch (Exception ex) {
            System.out.println("test1 - catch : " + ++x);
            return x + 10;
        } finally {
            System.out.println("test1 - finally : " + ++x);
            return x + 100;
        }
    }

输出结果

test1 - 1
test1 - catch : 2
test1 - finally : 3
main 结果:103

上面的两个实例可以看出不论是try还是cathc里面return都没被截胡最终返回的是finally中的return。

如果是异常同样也会被截胡

java
   public static int x = 0;

    public static void main(String[] args) {
        System.out.println("main 结果:" + test1());
    }

    public static int test1() {
        try {
            throw new RuntimeException("test1 - exception");
        } catch (Exception ex) {
            throw new RuntimeException("test1 - catch: " + ++x);
        } finally {
            throw new RuntimeException("test1 - finally: " + ++x);
        }
    }

建议

不要羞于传递异常,很多时候我们都觉得应该捕获全部的异常,如果每一种异常每一次异常都在程序收手动捕获和处理会导致程序非常臃肿且降低效率,其实这种情况下传递异常比捕获异常更好用,让高层次的方法统一捕获异常。

总结:

1. finally是在控制转移语句(return、throw)之前执行。
2. 如果finally中控制转移语句会导致try- catch中控制转移失效。
3.try-cath最好用在对异常的统一处理,不要做return等边界操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值