温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java的try、catch、finally语句中有return各类情况是什么

发布时间:2021-11-01 16:15:15 来源:亿速云 阅读:134 作者:iii 栏目:编程语言

本篇内容主要讲解“Java的try、catch、finally语句中有return各类情况是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java的try、catch、finally语句中有return各类情况是什么”吧!

一、try-catch 语句块

我们可以看看下面程序:

public static void main(String[] args) {      System.out.println(handleException0());   }    /**    * try,catch都有return    * @return    */   private static String handleException0() {     try{       System.out.println("try开始");       String s = null;       int length = s.charAt(0);       System.out.println("try结束");       return "try块的返回值";     }catch (Exception e){       System.out.println("捕获到了异常");       return "catch的返回值";     }   }

执行结果:

try开始 捕获到了异常 catch的返回值

分析:程序首先执行 try 块里面的代码,try 块里面发现有异常,try 块后面的代码不会执行(自然也不会return),然后进入匹配异常的那个  catch 块,然后进入 catch 块里面将代码执行完毕,当执行到 catch 里面的return 语句的时候,程序中止,然后将此 return  的最终结果返回回去。

二、try-catch-finally 语句块

这种语法块我分为了 4 种情况讨论,下面进行一一列举。

第一种情况,try 块里面有 return 的情况,并且捕获到异常

例1:

public static void main(String[] args) {   String result = handleException1();   System.out.println(result); } private static String handleException1() {   try{     System.out.println("try开始");     String str = null;     int length = str.length();     System.out.println("try结束");   }catch (Exception e){     System.out.println("捕获到了异常");   }finally {     System.out.println("finally块执行完毕了");   }   return "最终的结果"; }

例1执行的结果如下:

try开始 捕获到了异常 finally块执行完毕了 最终的结果

例2:

public static void main(String[] args) {   String result = handleException2();   System.out.println(result); } private static String handleException2() {   try{     System.out.println("try开始");     String str = null;     int length = str.length();     System.out.println("try结束");     return "try块的返回值";   }catch (Exception e){     System.out.println("捕获到了异常");   }finally {     System.out.println("finally块执行完毕了");   }   return "最终的结果"; }

例2的执行结果如下:

try开始 捕获到了异常 finally块执行完毕了 最终的结果

分析:首先 例1 和 例2 的结果是很显然的,当遇到异常的时候,直接进入匹配到相对应的 catch 块,然后继续执行 finallly 语句块,最后将  return 结果返回回去。

第二种情况:try块里面有return的情况,但是不会捕获到异常

例3:

思考:下面代码try语句块中有return语句,那么是否执行完try语句块就直接return退出方法了呢?

public static void main(String[] args) {   String result = handleException3();   System.out.println(result); } private static String handleException3() {   try{       System.out.println("");     return "try块的返回值";   }catch (Exception e){     System.out.println("捕获到了异常");   }finally {     System.out.println("finally块执行完毕了");   }   return "最终的结果"; }

例3的执行结果如下:

finally块执行完毕了 try块的返回值

分析:例3的结果其实我们可以通过打断点的方式去看看程序的具体执行流程,通过打断点我们可以发现,代码先执行 try块 里的代码,当执行到 return  语句的时候,handleException3方法并没有立刻结束,而是继续执行finally块里的代码,finally块里的代码执行完后,紧接着回到 try 块的  return 语句,再把最终结果返回回去, handleException 方法执行完毕。

第三种情况:try块和finally里面都有return的情况

例4:

public static void main(String[] args) {     System.out.println(handleException4());   }    /**    * 情况3:try和finally中均有return    * @return    */   private static String handleException4() {     try{       System.out.println("");       return "try块的返回值";     }catch (Exception e){       System.out.println("捕获到了异常");     }finally {       System.out.println("finally块执行完毕了");       return "finally的返回值";     }   //  return "最终的结果";//不能再有返回值   }

例4的执行结果:

finally块执行完毕了 finally的返回值

分析:需要注意的是,当 try 块和 finally 里面都有 return 的时候,在 try/catch/finally  语法块之外不允许再有return 关键字。我们还是通过在程序中打断点的方式来看看代码的具体执行流程。代码首先执行 try 块 里的代码,当执行到 return  语句的时候,handleException4 方法并没有立刻结束,而是继续执行 finally 块里的代码,当发现 finally 块里有 return  的时候,直接将 finally 里的返回值(也就是最终结果)返回回去, handleException4 方法执行完毕。

第四种情况:try块,catch块,finally块都有return

例5:

public static void main(String[] args) {     System.out.println(handleException5());   }    /**    * 情况4:try,catch,finally都有return    * @return    */   private static String handleException5() {     try{       System.out.println("try开始");       int[] array = {1, 2, 3};       int i = array[10];       System.out.println("try结束");       return "try块的返回值";     }catch (Exception e){       e.printStackTrace();//这行代码其实就是打印输出异常的具体信息       System.out.println("捕获到了异常");       return "catch的返回值";     }finally {       System.out.println("finally块执行完毕了");       return "finally的返回值";     } //    return "最终的结果";   }

例5的执行结果:

try开始 捕获到了异常 finally块执行完毕了 finally的返回值  java.lang.ArrayIndexOutOfBoundsException: 10 at  com.example.javabasic.javabasic.ExceptionAndError.TryCatchFinally.handleException5(TryCatchFinally.java:25) at  com.example.javabasic.javabasic.ExceptionAndError.TryCatchFinally.main(TryCatchFinally.java:14)

分析:程序首先执行try块里面的代码,try块里面发现有异常,try块后面的代码不会执行(自然也不会return),然后进入匹配异常的那个catch块,然后进入catch块里面将代码执行完毕,当执行到catch里面的return语句的时候,程序不会马上终止,而是继续执行finally块的代码,最后执行finally里面的return,然后将此return的最终结果返回回去。

到此,相信大家对“Java的try、catch、finally语句中有return各类情况是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI