Странное поведение Java при возврате из блока finally

Попробуйте этот фрагмент кода. Почему getValueB ()возвращает 1 вместо 2? В конце концов, функция приращения ()вызывается дважды.

    public class ReturningFromFinally
    {
      public static int getValueA() // This returns 2 as expected
      {
         try     { return 1; }
         finally { return 2; }
      }

      public static int getValueB() // I expect this to return 2, but it returns 1
      {
        try     { return increment(); }
        finally { increment(); }
      }

      static int counter = 0;

      static int increment()
       {
          counter ++;
          return counter;
       }

      public static void main(String[] args)
      {
          System.out.println(getValueA()); // prints 2 as expected
          System.out.println(getValueB()); // why does it print 1?
      }
}
10
задан CodeBlue 5 August 2014 в 15:20
поделиться