关于try-catch异常处理中代码执行顺序

首先我这里的测试方法是这样的:

public int testReturnInTryAndCatchAndFinally(Boolean hasException) {
        int a = 1;
        try {
            a = a + 2;
            System.out.println("try-exception-pre");
            if (hasException) {
                int x = 1/0;
            }
            a = a + 4;
            System.out.println("try-exception-after");
            return a;
        } catch (Exception e) {
            a = a + 8;
            System.out.println("catch");
            return a;
        } finally {
            a = a + 16;
            System.out.println("finally");
            return a;
        }
        //return a; todo finally中有return则这个return到达不了
    }

这是用来测试有return,且return在try块、catch块、finally块中都有的情况,每个块中都对变量a加了不同的值,并打印当前代码所在块(我分为了四个部分,分别是try-exception-pre、try-exception-after、catch、finally这四个部分分别对用try块中异常之前的代码、try块中异常之后的代码、catch中的代码、finally中的代码),我通过打印信息和a的最终返回值判断代码执行顺序和return的执行顺序。

这里直接上全部的测试代码:

​
public class TestTryCatchReturnOrder {

    @Test
    public void test() {
        /**
         * Try、Catch、Finally中其中一个有return
         */
        //todo try和catch或return中,无异常且return在try中走try-exception-after,有异常且return在catch中走catch,其余走异常体外
        int i1 = testReturnInTry(false);
        System.out.println(i1);//运行顺序try-exception-pre -> try-exception-after -> finally,返回的是try中的return:1+2+4=7
        int i2 = testReturnInTry(true);
        System.out.println(i2);//运行顺序try-exception-pre -> catch -> finally,返回的是异常体外的return:1+2+8+16=27
        int i3 = testReturnInCatch(false);
        System.out.println(i3);//运行顺序try-exception-pre -> try-exception-after -> finally,返回的是异常体外的return:1+2+4+16=23
        int i4 = testReturnInCatch(true);
        System.out.println(i4);//运行顺序try-exception-pre -> catch -> finally,返回的是try中的return:1+2+8=11
        //todo finally中有return,肯定返回finally中的return,无异常走try-exception-after,有异常走catch
        int i5 = testReturnInFinally(false);
        System.out.println(i5);//运行顺序try-exception-pre -> try-exception-after -> finally,返回的是finally中的return:1+2+4+16=23
        int i6 = testReturnInFinally(true);
        System.out.println(i6);//运行顺序try-exception-pre -> catch -> finally,返回的是finally中的return:1+2+8+16=27


        /**
         * Try、Catch、Finally中其中两个有return
         */
        //todo try和catch中有return,无异常走try-exception-after,有异常走catch
        int i7 = testReturnInTryAndCatch(false);
        System.out.println(i7);//运行顺序try-exception-pre -> try-exception-after -> finally,返回的是try中的return:1+2+4=7
        int i8 = testReturnInTryAndCatch(true);
        System.out.println(i8);//运行顺序try-exception-pre -> catch -> finally,返回的是catch中的return:1+2+8=11
        int i9 = testReturnInTryAndFinally(false);
        //todo finally中有return,肯定返回finally中的return,无异常走try-exception-after,有异常走catch
        System.out.println(i9);//运行顺序try-exception-pre -> try-exception-after -> finally,返回的是finally中的return:1+2+4+16=23
        int i10 = testReturnInTryAndFinally(true);
        System.out.println(i10);//运行顺序try-exception-pre -> catch -> finally,返回的是finally中的return:1+2+8+16=27
        int i11 = testReturnInCatchAndFinally(false);
        System.out.println(i11);//运行顺序try-exception-pre -> try-exception-after -> finally,返回的是finally中的return:1+2+4+16=23
        int i12 = testReturnInCatchAndFinally(true);
        System.out.println(i12);//运行顺序try-exception-pre -> catch -> finally,返回的是finally中的return:1+2+8+16=27


        /**
         * Try、Catch、Finally中都有return
         */
        //todo finally中有return,肯定返回finally中的return,无异常走try-exception-after,有异常走catch
        int i13 = testReturnInTryAndCatchAndFinally(false);
        System.out.println(i13);//运行顺序try-exception-pre -> try-exception-after -> finally,返回的是finally中的return:1+2+4+16=23
        int i14 = testReturnInTryAndCatchAndFinally(true);
        System.out.println(i14);//运行顺序try-exception-pre -> catch -> finally,返回的是finally中的return:1+2+8+16=27
    }

    public int testReturnInTry(Boolean hasException) {
        int a = 1;
        try {
            a = a + 2;
            System.out.println("try-exception-pre");
            if (hasException) {
                int x = 1/0;
            }
            a = a + 4;
            System.out.println("try-exception-after");
            return a;
        } catch (Exception e) {
            a = a + 8;
            System.out.println("catch");
        } finally {
            a = a + 16;
            System.out.println("finally");
        }
        return a;//todo 只在try中或catch中有return时,需要异常体外存在return
    }

    public int testReturnInCatch(Boolean hasException) {
        int a = 1;
        try {
            a = a + 2;
            System.out.println("try-exception-pre");
            if (hasException) {
                int x = 1/0;
            }
            a = a + 4;
            System.out.println("try-exception-after");
        } catch (Exception e) {
            a = a + 8;
            System.out.println("catch");
            return a;
        } finally {
            a = a + 16;
            System.out.println("finally");
        }
        return a;//todo 只在try中或catch中有return时,需要异常体外存在return
    }

    public int testReturnInFinally(Boolean hasException) {
        int a = 1;
        try {
            a = a + 2;
            System.out.println("try-exception-pre");
            if (hasException) {
                int x = 1/0;
            }
            a = a + 4;
            System.out.println("try-exception-after");
        } catch (Exception e) {
            a = a + 8;
            System.out.println("catch");
        } finally {
            a = a + 16;
            System.out.println("finally");
            return a;
        }
        //return a; todo finally中有return则这个return到达不了
    }

    public int testReturnInTryAndCatch(Boolean hasException) {
        int a = 1;
        try {
            a = a + 2;
            System.out.println("try-exception-pre");
            if (hasException) {
                int x = 1/0;
            }
            a = a + 4;
            System.out.println("try-exception-after");
            return a;
        } catch (Exception e) {
            a = a + 8;
            System.out.println("catch");
            return a;
        } finally {
            a = a + 16;
            System.out.println("finally");
        }
        //return a; todo try和catch中都有return则这个return到达不了
    }

    public int testReturnInTryAndFinally(Boolean hasException) {
        int a = 1;
        try {
            a = a + 2;
            System.out.println("try-exception-pre");
            if (hasException) {
                int x = 1/0;
            }
            a = a + 4;
            System.out.println("try-exception-after");
            return a;
        } catch (Exception e) {
            a = a + 8;
            System.out.println("catch");
        } finally {
            a = a + 16;
            System.out.println("finally");
            return a;
        }
        //return a; todo finally中有return则这个return到达不了
    }

    public int testReturnInCatchAndFinally(Boolean hasException) {
        int a = 1;
        try {
            a = a + 2;
            System.out.println("try-exception-pre");
            if (hasException) {
                int x = 1/0;
            }
            a = a + 4;
            System.out.println("try-exception-after");
        } catch (Exception e) {
            a = a + 8;
            System.out.println("catch");
            return a;
        } finally {
            a = a + 16;
            System.out.println("finally");
            return a;
        }
        //return a; todo finally中有return则这个return到达不了
    }
    public int testReturnInTryAndCatchAndFinally(Boolean hasException) {
        int a = 1;
        try {
            a = a + 2;
            System.out.println("try-exception-pre");
            if (hasException) {
                int x = 1/0;
            }
            a = a + 4;
            System.out.println("try-exception-after");
            return a;
        } catch (Exception e) {
            a = a + 8;
            System.out.println("catch");
            return a;
        } finally {
            a = a + 16;
            System.out.println("finally");
            return a;
        }
        //return a; todo finally中有return则这个return到达不了
    }
}

​

想自己跑一下测试的可以试一下,我这里直接总结:

没return时:

        无异常,则执行顺序是:try-exception-pre -> try-exception-after -> finally

        有异常,则执行顺序是:try-exception-pre -> catch -> finally

有return时:

        除开retrun这行代码,其余代码执行顺序还是与无return时相同,等非return代码全部按顺序执行一遍后,最后再根据刚才的执行顺序再执行一遍return。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值