1Z0-803 · Question #136
Which four statements are true regarding exception handling in Java?
The correct answer is B. A try block must be followed by either a catch or finally block D. A single catch block can handle more than one type of exception E. A checked exception must be caught explicitly F. In a catch block than can handle more than one exception, the subclass exception handler must. This question tests knowledge of core Java exception handling rules, including the structure of try-catch-finally blocks, the multi-catch feature, the concept of checked exceptions, and type hierarchy constraints in catch blocks.
Question
Which four statements are true regarding exception handling in Java?
Options
- AIn multicatch blocks, the subclass catch handler must be caught after the superclass catch
- BA try block must be followed by either a catch or finally block
- CThe Exception class is the superclass of all errors and exception in the Java language
- DA single catch block can handle more than one type of exception
- EA checked exception must be caught explicitly
- FIn a catch block than can handle more than one exception, the subclass exception handler must
How the community answered
(31 responses)- A3% (1)
- B94% (29)
- C3% (1)
Why each option
This question tests knowledge of core Java exception handling rules, including the structure of `try-catch-finally` blocks, the multi-catch feature, the concept of checked exceptions, and type hierarchy constraints in catch blocks.
For separate `catch` blocks, a subclass exception must be caught *before* its superclass exception to ensure it is handled specifically. Within a multi-catch, you cannot list a superclass and its subclass together if the subclass is already covered.
A `try` block in Java must be followed by at least one `catch` block to handle exceptions, a `finally` block to ensure code execution, or both, as it cannot exist standalone.
The `java.lang.Throwable` class is the superclass of all errors and exceptions in Java; `java.lang.Exception` is the superclass of all *exceptions*, but not of `Error` and its subclasses.
Java 7 introduced the multi-catch feature, allowing a single `catch` block to handle multiple exception types by listing them separated by a vertical bar, such as `catch (IOException | SQLException e)`.
Checked exceptions, which are subclasses of `java.lang.Exception` but not `RuntimeException`, must be explicitly handled either by surrounding the code in a `try-catch` block or by declaring them with a `throws` clause in the method signature.
In a multi-catch block, you cannot specify a supertype and a subtype where the subtype is already covered by the supertype (e.g., `catch (IOException | Exception e)` would be a compile-time error because `IOException` is a subclass of `Exception`).
Concept tested: Java exception handling rules and features
Source: https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html
Topics
Community Discussion
No community discussion yet for this question.