QuestionQ77

Handling Exceptions

Given the following code fragment:

Question Image

Which two changes should be made so that the code compiles successfully?

Choose two
  • A
  • B Replace line 7 with throw IOException (ג€Exception raisedג€);
  • C Replace line 11 with public static void main(String[]) args) throws Exception {
  • D At line 14, insert throw new IOException();
  • E Replace line 5 with public void printFileContent() throws IOException {
Explanation

IOException is a checked exception. printFileContent() must declare throws IOException because it throws that exception, and main must declare or catch the resulting exception when it invokes printFileContent(). Declaring throws Exception on main satisfies that caller requirement.

Community Discussion

No comments yet. Be the first to start the discussion!