开发者社区 问答 正文

java异常处理标准中出错的格式是什么呀?

已解决

java异常处理标准中出错的格式是什么呀?

展开
收起
游客gzyuldo4mrg6i 2022-04-03 20:55:22 966 分享 版权
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;
    
        } catch(Exception e) {
    
            throw e;
    
        } 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();
    
        }
    
    }
    

    }

    输出结果:

    1、除法计算开始

    2、除法计算结束

    java.lang.ArithmeticException...

    2022-04-03 21:13:20
    赞同 展开评论
问答分类:
问答标签:
问答地址: