返回语句在java异常处理 [英] return statement in java exception handling

查看:170
本文介绍了返回语句在java异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果执行不会引起异常,那么控制将终止。 JVM中忽略try块中的return语句?。或者如果异常发生,那么控件去捕获块也忽略返回状态和控制转到finally块并从最终返回

If execution doesn't cause exception then control goes to finally block. So is the return statement in try block is being ignored by JVM? . Or if exception occurs then control goes to catch block there also it ignored return statment and control go to finally block and return from finally

      public class Helper {
         public int showException(int a, int b){

               try{
               int c=a/b;
               return c;
               } catch(Exception e){
                    return 0;
               } finally{
                   return 3;
                 }
         }
      }


推荐答案

因为最终块将被执行,无论您是否输入尝试 catch ,我想这就是为什么它最终被称为 :)

Because finally block will be executed every time whether you enter in try or in catch, I think that's why its called finally :)

FROM JAVA DOCS


finally块总是在try块退出时执行。
确保即使出现意外的
异常,也会执行finally块。

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.



注意:它赢了只有当



Note: It won't be executed only when


如果JVM在执行try或catch代码时退出,那么
finally块可能无法执行。同样地,如果执行
的线程中断或捕获代码被中断或者被杀死,即使应用程序整体继续,finally块也可能执行

If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.

这篇关于返回语句在java异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆