开发者社区> 问答> 正文

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

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

展开
收起
xenny 2018-12-13 16:29:22 1856 0
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
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载