1Z0-809 · Question #69
View the exhibit. Given the code fragment: Which change enables the code fragment to print the following in the event of missing info or age out of range? James age: 20 Williams age: 32
The correct answer is B. Replacing line 5 with public static void main (String [] args) throws.Exception {. Option B works because Exception is the superclass of all checked exceptions in Java, so declaring throws Exception on main covers both MissingInfoException and AgeOutOfRangeException (and any other checked exception) with a single, valid declaration - satisfying the compiler…
Question
Options
- AReplacing line 5 with public static void main (String [] args) throws MissingInfoException, AgeOutOfRangeException {
- BReplacing line 5 with public static void main (String [] args) throws.Exception {
- CEnclosing line 6 and line 7 within a try block and adding: catch (Exception e1) { //code goes here } catch (MissingInfoException e2) { //code goes here} catch (AgeOutOfRangeException e3) { //code goes here}
- DEnclosing line 6 and line 7 within a try block and adding: catch (missingInfoException e2) { //code goes here} catch (AgeOutOfRangeException e3) { //code goes here}
How the community answered
(41 responses)- A2% (1)
- B83% (34)
- C10% (4)
- D5% (2)
Explanation
Option B works because Exception is the superclass of all checked exceptions in Java, so declaring throws Exception on main covers both MissingInfoException and AgeOutOfRangeException (and any other checked exception) with a single, valid declaration - satisfying the compiler without requiring separate listing.
Option A is a trap: while listing both specific exceptions looks correct, the code fragment likely throws a third exception or has a scenario not covered by those two alone - throws Exception is the safe, all-encompassing alternative that A's narrower declaration misses.
Option C is wrong because it places catch (Exception e1) before the more specific exception types - Java requires specific exceptions to be caught before general ones; catching Exception first causes a compile error because the subsequent catch blocks become unreachable.
Option D is a simple compile error: missingInfoException uses a lowercase m, but Java class names are case-sensitive, so the compiler won't recognize it as the MissingInfoException class.
Memory tip: Think of throws Exception as the "master key" - since all checked exceptions inherit from Exception, one declaration unlocks everything. When you see catch blocks, remember the rule "specific before general" (child before parent), or the compiler will block you.
Community Discussion
No community discussion yet for this question.