Java基础系列:try...catch...finally

1 return在try

package com.company.exception;

import static com.company.constant.StringConstant.*;

/**
 * 异常抛出及捕获测试
 * @author xindaqi
 * @since 2020-12-30
 */

public class ThrowsTest {

    /**
     * return在try
     *
     * @param a 入参
     * @return 处理结果
     */
    private static StringBuilder returnInTry(int a) {
        StringBuilder str = new StringBuilder(10);
        try{
            int b = 1/a;
            str.append(TRY);
            System.out.println(FROM + str);
            return str.append(TRY);
        } catch(Exception e) {
            str.append(CATCH);
            System.out.println(FROM + str);
        } finally{
            str.append(FINALLY);
            System.out.println(FROM + str);
        }
        return str.append(END);
    }

    private static void printLine() {
        System.out.println("============");
    }

    public static void main(String[] args) {
        StringBuilder str = returnInTry(1);
        System.out.println(RESULT + str);
        printLine();
        StringBuilder str1 = returnInTry(0);
        System.out.println(RESULT + str1);
        printLine();
       
    }
}
  • 结果
from:try->
from:try->try->finally!
result:try->try->finally!
============
from:catch->
from:catch->finally!
result:catch->finally!
============

2 return在try和catch

package com.company.exception;

import static com.company.constant.StringConstant.*;

/**
 * 异常抛出及捕获测试
 * @author xindaqi
 * @since 2020-12-30
 */

public class ThrowsTest {

    /**
     * return在try和catch
     *
     * @param a 入参
     * @return 处理结果
     */
    private static StringBuilder returnInTryAndCatch(int a) {
        StringBuilder str = new StringBuilder(10);
        try{
            int b = 1/a;
            str.append(TRY);
            System.out.println(FROM + str);
            return str.append(TRY);
        } catch(Exception e) {
            str.append(CATCH);
            System.out.println(FROM + str);
            return str.append(CATCH);
        } finally{
            str.append(FINALLY);
            System.out.println(FROM + str);
        }
    }

    private static void printLine() {
        System.out.println("============");
    }

    public static void main(String[] args) {
       
        StringBuilder str2 = returnInTryAndCatch(1);
        System.out.println(RESULT + str2);
        printLine();
        StringBuilder str3 = returnInTryAndCatch(0);
        System.out.println(RESULT + str3);
        printLine();
        
    }
}
  • 结果
from:try->
from:try->try->finally!
result:try->try->finally!
============
from:catch->
from:catch->catch->finally!
result:catch->catch->finally!
============

3 return在try/catch和finally

package com.company.exception;

import static com.company.constant.StringConstant.*;

/**
 * 异常抛出及捕获测试
 * @author xindaqi
 * @since 2020-12-30
 */

public class ThrowsTest {

 
    /**
     * return在try、catch和finally
     *
     * @param a 入参
     * @return 处理结果
     */
    private static StringBuilder returnInTryAndCatchAndFinally(int a) {
        StringBuilder str = new StringBuilder(10);
        try{
            int b = 1/a;
            str.append(TRY);
            System.out.println(FROM + str);
            return str.append(TRY);
        } catch(Exception e) {
            str.append(CATCH);
            System.out.println(FROM + str);
            return str;
        } finally{
            str.append(FINALLY);
            System.out.println(FROM + str);
            return str.append(FINALLY);
        }
    }

    private static void printLine() {
        System.out.println("============");
    }

    public static void main(String[] args) {
       
        StringBuilder str4 = returnInTryAndCatchAndFinally(1);
        System.out.println(RESULT + str4);
        printLine();
        StringBuilder str5 = returnInTryAndCatchAndFinally(0);
        System.out.println(RESULT + str5);
        printLine();
    }
}
  • 结果
from:try->
from:try->try->finally!
result:try->try->finally!finally!
============
from:catch->
from:catch->finally!
result:catch->finally!finally!
============

4 小结

  • try…catch…finally:finally一定会执行
  • 顺序执行所有可执行的表达式,包括return中的表达式
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值