I'm reading the
SCJP Study Guide book by Sierra and Bates. I had a question about one of the practice questions in the book. In chapter 5 practice question 15 is as follows:
And given the following four code fragments:
I. public static void main(
String[] args) {
II. public static void main(String[] args) throws Exception {
III. public static void main(String[] args) throws IOException {
IV. public static void main(String[] args) throws RuntimeException {
if the four fragments are inserted independently at line 4, which are true? (Choose all that apply.)
A - C are incorrect and I understand why.
D Some, but not all, will compile and execute and throw an exception. (True statement and I understand why)
E When considering fragments II, III, and IV, of those that will compile, adding a try/catch block around
line 6 will cause compilation to fail.
The book says that E is incorrect because it's okay to both handle and declare an exception. However, putting a try/catch block around line 6 is putting it around the closing curly brace of the class, which would cause a compile error. Am I missing something here? Or am I just interpreting the question wrong? Thanks in advance for your help.