Java Foundations — Question 20
Which statement is true about exception handling?
Answer options
- A. At least one catch block must accompany a try statement.
- B. All statements in a try block are executed, even if an exception occurs in the middle of the try block.
- C. At least one statement in a try block must throw an exception.
- D. All catch blocks must be ordered from general to most specific.
Correct answer: D
Explanation
The correct answer is D because in exception handling, catch blocks should be ordered from the most general exceptions to the most specific to ensure that the correct handler is invoked. Option A is incorrect because a try statement can exist without a catch block if there is a finally block. Option B is false as the execution of a try block stops as soon as an exception is thrown. Option C is incorrect because it's not mandatory for a try block to throw an exception.