nerdexam
Oracle

1Z0-819 · Question #161

import java.io.FileNotFoundException; import java.io.IOException; public class Tester { public static void main(String[] args) { doA(); //Line 1 } private static void doA() throws IOException…

The correct answer is A. catch(IOException e) {}. Option A is correct because doA() declares throws IOException, which is a checked exception - the compiler requires it to be caught (or re-declared) in any caller that doesn't propagate it. Catching IOException alone is sufficient because FileNotFoundException is a subclass of…

Exception Handling

Question

import java.io.FileNotFoundException; import java.io.IOException; public class Tester { public static void main(String[] args) { doA(); //Line 1 } private static void doA() throws IOException, IndexOutOfBoundsException { if (false) { throw new FileNotFoundException(); } else { throw new IndexOutOfBoundsException(); } } } What must be added in line 1 to compile this class?

Options

  • Acatch(IOException e) {}
  • Bcatch(FileNotFoundException |IndexOutOfBoundsException e) {}
  • Ccatch(IndexOutOfBoundsException |IOException e) {}
  • Dcatch(IndexOutOfBoundsException e) {} catch(FileNotFoundException e) {}
  • Ecatch(IndexOutOfBoundsException e) {}catch(IndexOutOFBou dsException e) {}

How the community answered

(35 responses)
  • A
    77% (27)
  • B
    6% (2)
  • C
    11% (4)
  • D
    3% (1)
  • E
    3% (1)

Explanation

Option A is correct because doA() declares throws IOException, which is a checked exception - the compiler requires it to be caught (or re-declared) in any caller that doesn't propagate it. Catching IOException alone is sufficient because FileNotFoundException is a subclass of IOException, so it's already covered. IndexOutOfBoundsException is an unchecked exception (it extends RuntimeException), meaning the compiler never requires it to be caught, so no catch for it is needed.

Why the distractors fail:

  • B catches FileNotFoundException instead of IOException - the compiler sees IOException in the throws clause and won't accept a narrower subtype as a substitute; uncaught IOException still causes a compile error.
  • C catches both IndexOutOfBoundsException | IOException - while IOException would satisfy the compiler, catching an unchecked exception alongside it in a multi-catch is unnecessary; the exam marks this as not the required solution (minimal, clean handling is expected).
  • D catches IndexOutOfBoundsException and FileNotFoundException separately - again, FileNotFoundException doesn't cover the declared IOException type, so the compiler still complains about unhandled IOException.
  • E attempts two catch blocks for the same exception type (IndexOutOfBoundsException twice), which is always a compile error regardless of the typo.

Memory tip: Think "Checked = Caught" - only checked exceptions (those NOT extending RuntimeException) trigger compile errors when uncaught. If a method declares throws SomeCheckedException, your caller must catch that exact type or a supertype - a subtype alone won't satisfy the compiler.

Topics

#Checked Exceptions#Exception Hierarchy#Try-Catch Blocks#IOException

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice