今天遇到个题,有点不明白,各位大虾帮帮忙
原题:
public class A{
String get(){
  try{
   return "a";
  }
   finally{
     return "b";
   }
 }
   public static void main(String args[]){
       A a = new A();
      System.out.println(a.get)   }
}程序运行结果为  b
但是问什么呢????

解决方案 »

  1.   

    finally  是总会 执行到的,不管你 return了还是catch到异常了
      

  2.   

    finally语句块总是在控制权离开try语句块的时候才执行,无论try块是正常结束还是意外结束,最后都会执行finally块中的语句
    除非你的try块直接System.exit();
    还有不要让受检查传播到finally块外。
      

  3.   

    public class A {
    String get(){
      try{
      System.exit(0);
      }
      finally{
        return "b";
      }
    }
      public static void main(String args[]){
          A a = new A();
          System.out.println(a.get());  //这边楼主写错了,方法忘记加()了。   } }对System.exit(0);程序退出是不执行的。
      

  4.   

    恩,谢谢提醒。
    我知道finally会最后执行,呵呵。
    是不是finally中的return语句将前面try中的return覆盖,最后只输出 b。谢谢。。
      

  5.   

    最好不要在finally里写return如果在finally里写return eclipse会给出warning:finally block does not complete normally