try catch 嵌套

try catch 嵌套实践测试在单元测试中写入以下方法:testMain()主方法,out()里面嵌套了两层trycatch异常代码写在内层try中示例一:@TestpublicvoidtestMain(){out();log.info(“后续处理业务”);}publicvoidout(){//外层trytry{System.out.println(“外层输出”);

大家好,欢迎来到IT知识分享网。

实践测试

在单元测试中写入以下方法:
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 代码块中,只需在上层方法中进行异常处理即可

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/20656.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信