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 ...
Get Java™ Puzzlers: Traps, Pitfalls, and Corner Cases now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.