当前位置 : 主页 > 编程语言 > java >

Java中的finally中的return

来源:互联网 收集:自由互联 发布时间:2022-07-17
先引出一道题 执行下列代码的输出结果是( ) public class Demo { public static void main ( String args []){ int num = 10 ; System . out . println ( test ( num )); } public static int test ( int b ){ try { b += 10 ; return b ;


先引出一道题

执行下列代码的输出结果是( )

public class Demo{
 public static void main(String args[]){
   int num = 10;
   System.out.println(test(num));
}
public static int test(int b){
   try
   {
    b += 10;
    return b;
   }
   catch(RuntimeException e)
   {
   }
   catch(Exception e2)
   {
   }
   finally
   {
    b += 10;
    return b;
   }
  }
}

解析

关于try catch 知识:程序运行到 try块,b=20;并没有发生异常,不运行catch块,运行到return b;因为finally块无论如何都要运行,因此并不发生返回动作,进行运行finally块,b=30;
进行程序返回输出;


上一篇:Java性能压测-性能监控-jvisualvm使用
下一篇:没有了
网友评论