开发者社区 问答 正文

[@小川游鱼][¥20]try catch finally,try里有return,finally还执行么?

try catch finally,try里有return,finally还执行么?

展开
收起
xenny 2018-12-13 16:29:22 1924 分享
分享
版权
举报
1 条回答
写回答
取消 提交回答
  • 执行的

    public static void main(String[] args)  {
    
        try {
            String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z",
                    Locale.ENGLISH).format(new Date());
            System.out.println(str);
            return;
        } catch (Exception e) {
            System.out.println("Exception!");
        } finally {
            System.out.println("Finally!");
        }
    }
    

    输出:

    2019-01-14 17:05:23.169 +0800
    Finally!


    执行的

    public static void main(String[] args)  {
    
        try {
            String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z",
                    Locale.ENGLISH).format(new Date());
            System.out.println(str);
            return;
        } catch (Exception e) {
            System.out.println("Exception!");
        } finally {
            System.out.println("Finally!");
        }
    }
    

    输出:

    2019-01-14 17:05:23.169 +0800
    Finally!


    执行的

    2019-07-17 23:20:47 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
问答地址: