Java Magazine, Jan/Feb 2018
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2018 109 fix this If you want to examine this effect try running this code String names Tony Jane Immut5 i5 new Immut5 names System out println i5 get 0 i5 get 0 names 0 Anthony System out println i5 get 0 i5 get 0 Answer 3 The correct answer is option D This question investigates the rules and purpose of Javas exception mechanism and it also dares to stray into that troublesome territory of asking whats best rather than merely whats correct However we hope to make a good case for that value judgment and while we are happy to include this question because it creates a useful discussion both about Javas exception mechanism and about how to evaluate a judgment like this we doubt that this question would survive unchanged in the real exam The first point is that Java distinguishes checked exceptions from unchecked exceptions and errors In particular a method that might throw a checked exception must announce this in that methods signature In this question the appropriate point for the syntax that declares such information is marked point A Therefore the question is really asking what exception declaration would best suit this method Its pretty clear that any situation that doesnt even compile cannot be considered satisfactory so as long as some of the options would compile they must be better than any that do not Consider the issue of compilation first If the method might throw any checked exceptions it must carry a declaration that announces that In this case the only checked exception that is potentially thrown is the IOException so at a minimum the method must declare something that encompasses that exception Options B and E fail on that point because they declare unchecked exceptions Note that OutOfMemoryError falls into the category of unchecked although its a subclass of Throwable not of Exception For convenience well simply use the term unchecked exceptions to include errors that are not parents of IOException Therefore options B and E are incorrect
You must have JavaScript enabled to view digital editions.