O'Reilly logo

Java™ Puzzlers: Traps, Pitfalls, and Corner Cases by Neal Gafter, Joshua Bloch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5. Exceptional Puzzlers

The puzzles in this chapter concern exceptions and the closely related try-finally statement. A word of caution: Puzzle 44 is exceptionally difficult.

Puzzle 36: Indecision

This poor little program can’t quite make up its mind. The decision method returns true. But it also returns false. What does it print? Is it even legal?

public class Indecisive {    public static void main(String[ ] args) {        System.out.println(decision( ) );    }    static boolean decision( ) {        try {            return true;        } finally {            return false;        }    }}

Solution 36: Indecision

You might think that this program is illegal. After all, the decision method can’t return both true and false. If you tried ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required