...and people are still using java? (Answers)

by Manu (modified: 2009 Sep 23)

Here are the answers for the quiz of last week:

  1. b=2
  2. b=2
  3. b=2 and an exception
  4. b=4 and no exception
  5. returns 2
  6. returns 1 and b=2

The difficult ones where definitely the last 3. Note that for #4 this is rejected by C# which forbids exiting from the finally clause (actually this is a .NET runtime restriction that has moved up to the language specification).

If you are unlucky and have to program in Java (instead of Eiffel as I do), you have to be careful when handling code that makes an extensive use of try .. finally... with goto style code flow.

  • Peter Gummer (14 years ago 23/9/2009)

    The jump instructions are the real problem

    I don't think finally is the problem. It's the jump instructions: break, return, etc.

    Everyone knows that Dijkstra said that goto was considered harmful, but hardly anyone these days (apart from we Eiffel programmers) seems to remember that these jump instructions are unstructured too.

    • Colin LeMahieu (14 years ago 23/9/2009)

      My most recent head-slapping moment came when I saw this in some code.

      do { //.... if (...) break; //---- } while (false);

  • cfd (14 years ago 22/1/2010)

    Your criticism of Java is just nitpicking at trivia

    If this (the surprises surrounding break/continue/return inside a finally clause) were the worst defect in Java, then Java would be a pretty friggin' amazing programming language.

    In other words, you are focusing in on minutae and losing sight of the bigger picture.

    • Manu (14 years ago 23/1/2010)

      Of course, this is mostly intended for our Eiffel users to have a little bit of fun. We all know the benefit of using Eiffel over the other approaches and that there is not just one reason for using Eiffel but many.