0


try catch 嵌套

实践测试

在单元测试中写入以下方法:

testMain()

主方法,

out()

里面嵌套了两层try catch
异常代码写在内层try中

示例一:

    @Test
    public void testMain(){
        out();
        log.info("后续处理业务");
    }

    public void out(){
        //外层try
        try {
            System.out.println("外层输出");
            //内层try
            try {
                int i = 2/0; //异常代码
            }catch (Exception e){
                log.error("内层异常",e);
            }

        }catch (Exception e){
            log.error("外层异常",e);
        }
    }

日志信息:
在这里插入图片描述

外层输出
20:38:27.172 [main] ERROR com.eshore.paas.ops.serv.secondalert.secondAlertTest - 内层异常
java.lang.ArithmeticException: / by zero
    at com.eshore.paas.ops.serv.secondalert.secondAlertTest.out(secondAlertTest.java:52)
    at com.eshore.paas.ops.serv.secondalert.secondAlertTest.testMain(secondAlertTest.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
20:38:27.179 [main] INFO com.eshore.paas.ops.serv.secondalert.secondAlertTest - 后续处理业务

可以看到,内层try中的异常由

内层catch处理

了,外层catch并未处理,主方法后续业务逻辑并未受到影响,可正常执行.

示例二:

内层try catch中添加

throw new RuntimeException("内层抛出异常",e)

将异常抛出

    @Test
    public void testMain(){
        out();
        log.info("后续处理业务");
    }

    public void out(){
        //外层try
        try {
            System.out.println("外层输出");
            //内层try
            try {
                int i = 2/0;
            }catch (Exception e){
                log.error("内层异常",e);
                //向外抛出异常
                throw new RuntimeException("抛出异常",e);
            }

        }catch (Exception e){
            log.error("外层异常",e);
        }
    }

日志信息:
在这里插入图片描述

外层输出
20:47:15.890 [main] ERROR com.eshore.paas.ops.serv.secondalert.secondAlertTest - 内层异常
java.lang.ArithmeticException: / by zero
    at com.eshore.paas.ops.serv.secondalert.secondAlertTest.out(secondAlertTest.java:52)
    at com.eshore.paas.ops.serv.secondalert.secondAlertTest.testMain(secondAlertTest.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
20:47:15.894 [main] ERROR com.eshore.paas.ops.serv.secondalert.secondAlertTest - 外层异常
java.lang.RuntimeException: 内层抛出异常
    at com.eshore.paas.ops.serv.secondalert.secondAlertTest.out(secondAlertTest.java:55)
    at com.eshore.paas.ops.serv.secondalert.secondAlertTest.testMain(secondAlertTest.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.ArithmeticException: / by zero
    at com.eshore.paas.ops.serv.secondalert.secondAlertTest.out(secondAlertTest.java:52)
    ... 23 common frames omitted
20:47:15.894 [main] INFO com.eshore.paas.ops.serv.secondalert.secondAlertTest - 后续处理业务

内层try catch

向外抛出异常

以后,外层try catch

捕获了异常

,并打印日志。主方法中后续处理业务逻辑正常执行。

示例三:

在外层try catch中再次将异常抛出

throw new RuntimeException("外层抛出异常",e);
    @Test
    public void testMain(){
        out();
        log.info("后续处理业务");
    }

    public void out(){
        //外层try
        try {
            System.out.println("外层输出");
            //内层try
            try {
                int i = 2/0;
            }catch (Exception e){
                log.error("内层异常",e);
                throw new RuntimeException("内层抛出异常",e);
            }

        }catch (Exception e){
            log.error("外层异常",e);
            //向外抛出异常
            throw new RuntimeException("外层抛出异常",e);
        }
    }

日志信息:
在这里插入图片描述

可以看到,程序运行错误。
因为out()方法外层try catch

抛出异常

以后,主方法并

未进行处理

导致的

改进:
在主方法中,对out()

添加一层try catch

进行异常处理:

    @Test
    public void testMain(){
    //添加异常处理
        try {
            out();
        }catch (Exception e){
            log.info("处理out()方法异常",e);
        }

        log.info("后续处理业务");
    }

    public void out(){
        //外层try
        try {
            System.out.println("外层输出");
            //内层try
            try {
                int i = 2/0;
            }catch (Exception e){
                log.error("内层异常",e);
                throw new RuntimeException("内层抛出异常",e);
            }

        }catch (Exception e){
            log.error("外层异常",e);
            throw new RuntimeException("外层抛出异常",e);
        }
    }

此时可以看到主方法处理异常以后,可以正常向下执行.
在这里插入图片描述

示例四

如果out()方法中的

异常代码不在try 代码块

中,且主方法也

没有

对out()方法进行

异常捕获处理

会怎样?
代码:

    @Test
    public void testMain(){
        out();
        log.info("后续处理业务");
    }

    public void out(){
        //添加异常代码,不在try 代码块中
        int j = 3/0;

        //外层try
        try {
            System.out.println("外层输出");
            //内层try
            try {
                int i = 2/0;
            }catch (Exception e){
                log.error("内层异常",e);
                throw new RuntimeException("内层抛出异常",e);
            }

        }catch (Exception e){
            log.error("外层异常",e);
            throw new RuntimeException("外层抛出异常",e);
        }
    }

日志:
在这里插入图片描述
程序直接报错

那么该如何处理?
在out()的

上层方法

即主方法中,

对out()进行异常处理
    @Test
    public void testMain(){
    //对out()进行异常处理
        try {
            out();
        }catch (Exception e){
            log.info("处理out()方法异常",e);
        }

        log.info("后续处理业务");
    }

    public void out(){
        int j = 3/0;

        //外层try
        try {
            System.out.println("外层输出");
            //内层try
            try {
                int i = 2/0;
            }catch (Exception e){
                log.error("内层异常",e);
                throw new RuntimeException("内层抛出异常",e);
            }

        }catch (Exception e){
            log.error("外层异常",e);
            throw new RuntimeException("外层抛出异常",e);
        }
    }

日志信息:
在这里插入图片描述
可以看出程序正常执行.

结论:

下层方法

中的异常代码

没有在try 代码块

中,只需在

上层方法

中进行

异常处理

即可

标签: java spring

本文转载自: https://blog.csdn.net/qq_44209563/article/details/123695520
版权归原作者 qq_44209563 所有, 如有侵权,请联系我们删除。

“try catch 嵌套”的评论:

还没有评论