try catch throws Exception

做一个大方法,要调用很多小方法,如果小方法中出现问题,要在大方法中打印日志,说明是哪个地方出了问题。


一般是通过判断返回值标志是否执行正确,但是有的程序是void。这样就可以try catch一下,如果出现问题,向上抛出异常到主方法中。


第一次:
public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            try {
                System.out.println(i + "循环");
                inNewException();
                System.out.println("我在异常后面---执行");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("外面抛异常");
                continue;
            }
        }
    }
    
    private static void inNewException() throws TestException {
        try {
            int a = 0;
            int b = 20 / a;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("内部抛异常");
        }
    }
打印日志:
0循环
java.lang.ArithmeticException: / by zero
at com.StudentController.inNewException(StudentController.java:482)
at com.StudentController.main(StudentController.java:460)
内部抛异常
我在异常后面---执行
1循环
java.lang.ArithmeticException: / by zero
at com.StudentController.inNewException(StudentController.java:482)
at com.StudentController.main(StudentController.java:460)
内部抛异常
我在异常后面---执行
2循环
java.lang.ArithmeticException: / by zero
at com.StudentController.inNewException(StudentController.java:482)
at com.StudentController.main(StudentController.java:460)
内部抛异常
我在异常后面---执行




通过日志会发现虽然inException方法异常了,但是主方法还会继续往下运行,在主方法中的try catch是捕获不到inException方法的异常的。(当然这里也可以通过返回值去判断内部方法是否异常了)


第二次:


public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            try {
                System.out.println(i + "循环");
                inException();
                System.out.println("我在异常后面---执行");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("外面抛异常");
                continue;
            }
        }
    }
    
    private static void inException() throws TestException {
        try {
            int a = 0;
            int b = 100 / a;
        } catch (Exception e) {
            throw new TestException("inException exception");
        }
    }


执行日志:


0循环
com.TestException: inException exception
外面抛异常
1循环
外面抛异常
2循环
外面抛异常
at com.StudentController.inException(StudentController.java:475)
at com.StudentController.main(StudentController.java:460)
com.TestException: inException exception
at com.StudentController.inException(StudentController.java:475)
at com.StudentController.main(StudentController.java:460)
com.TestException: inException exception
at com.StudentController.inException(StudentController.java:475)
at com.StudentController.main(StudentController.java:460)


可以看到都是在主方法中的catch 捕获到了异常,并且由于catch中的continue;for循环未中断,但是只执行到异常的位置,System.out.println("我在异常后面---执行");这个未打印出来。




第三次:




public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            try {
                inException();
                inNewException();
                System.out.println("我在异常后面---执行");
            } catch (TestException e) {
                System.out.println("外面抛异常:"+e.getMessage());
                continue;
            }catch (Exception e) {
                System.out.println("外面抛异常:"+e.getMessage());
                continue;
            }
        }
    }
    
    private static void inException() throws TestException {
        try {
            int a = 0;
            int b = 100 / a;
        } catch (Exception e) {
            throw new TestException("inException exception");
        }
    }
    
    private static void inNewException() throws TestException {
        try {
            int a = 0;
            int b = 100 / a;
        } catch (Exception e) {
            throw new TestException("inNewException exception");
        }
    }
日志:
外面抛异常:inNewException exception
外面抛异常:inNewException exception
外面抛异常:inNewException exception


调用n个方法,每个方法抛出自定义异常,但是message 不同 ,在主方法捕获异常时,可以通过e.getMessage不同,判断具体是哪个方法出现了问题。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值