开发者社区 问答 正文

java异常处理标准格式不建议缩写有哪些原因?

已解决

java异常处理标准格式不建议缩写有哪些原因?

展开
收起
游客gzyuldo4mrg6i 2022-04-03 21:13:20 1017 分享
分享
版权
举报
1 条回答
写回答
取消 提交回答
  • 推荐回答

    class MyMath {

    // 此时表示div()方法上如果出现了异常交给被调用处处理
    
    public static int div(int x, int y) throws Exception {
    
        int result = 0;
    
        System.out.println("***1、除法计算开始***");
    
        try {
    
            result = x / y;
    
        } finally {
    
            System.out.println("***2、除法计算结束***");
    
        }
    
        return result;
    
    }
    

    }

    public class TestDemo {

    public static void main(String args[]) {
    
        try {
    
            System.out.println(MyMath.div(10, 0));
    
        } catch(Exception e) {
    
            e.printStackTrace();
    
        }
    
    }
    

    }

    如果现在你直接使用了try...finally,那么表示你连处理一下的机会都没有,就直接抛出了。

    2022-04-03 21:43:30 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等