1Z0-803 · Question #174
View the exhibit. Given the code fragment: Which change enables the code to print the following? James age: 20 Williams age: 32
The correct answer is B. Replacing line 5 with public static void main (String [] args) throws Exception {. To enable the code to print the desired output by resolving potential checked exceptions, the main method signature should be updated to declare that it throws the general Exception type.
Question
View the exhibit. Given the code fragment:
Which change enables the code to print the following? James age: 20 Williams age: 32
Exhibits
Options
- AReplacing line 5 with public static void main (String [] args) throws MissingInfoException,
- BReplacing line 5 with public static void main (String [] args) throws Exception {
- CEnclosing line 6 and line 7 within a try block and adding:
- DEnclosing line 6 and line 7 within a try block and adding:
How the community answered
(58 responses)- A2% (1)
- B84% (49)
- C9% (5)
- D5% (3)
Why each option
To enable the code to print the desired output by resolving potential checked exceptions, the `main` method signature should be updated to declare that it throws the general `Exception` type.
Declaring `throws MissingInfoException` would only cover that specific checked exception; if other checked exceptions could be thrown by lines 6 and 7, the compilation would still fail.
If lines 6 and 7 potentially throw a checked exception like `MissingInfoException`, declaring `public static void main (String [] args) throws Exception` allows the program to compile by propagating any checked exception without requiring explicit `try-catch` blocks within the `main` method itself. `Exception` is the superclass of all checked exceptions, thus covering `MissingInfoException` and any other potential checked exceptions thrown.
Enclosing code in a `try` block requires a `catch` or `finally` block to handle exceptions, which is not fully specified here and might not be the simplest way to *enable* printing if simple propagation is acceptable.
This option, similar to C, is incomplete as it doesn't specify what must be 'added' after the `try` block to correctly handle or declare exceptions.
Concept tested: Java checked exceptions and throws declaration
Source: https://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.html
Topics
Community Discussion
No community discussion yet for this question.

