Given the following class, how many lines contain compilation errors?
package mypkg; // w ww.j ava 2 s . c om import java.io.*; class MyException extends Exception {} class MyClass implements Closeable { public void close() throws IOException {} } public class Main { public static void main(String... bees) { try (MyClass s = new MyClass(), MyClass t = new MyClass()) { throw new MyException(); } catch (Exception e) { } catch (MyException e) { } finally { } } }
B.
The code does not compile, so Option E is incorrect.
The first compilation error is in the try-with-resources declaration.
There are two resources being declared, which is allowed, but they are separated by a comma (,) instead of a semicolon (;).
The second compilation problem is that the order of exceptions in the two catch blocks are reversed.
Since Exception will catch all MyException
instances, the second catch block is unreachable.
For these two reasons, Option B is the correct answer.