A developer writes a method that reads a file and processes its contents. If the file does not exist, the method should notify the caller. Which exception should the method declare in its throws clause?
Trap 1: RuntimeException
RuntimeException is unchecked; the caller is not forced to handle it.
Trap 2: Exception
Exception is too general; should use a more specific subclass.
Trap 3: IOException
IOException is broader than needed; FileNotFoundException is more specific.
- A
RuntimeException
Why wrong: RuntimeException is unchecked; the caller is not forced to handle it.
- B
FileNotFoundException
FileNotFoundException is a checked exception specifically for missing files.
- C
Exception
Why wrong: Exception is too general; should use a more specific subclass.
- D
IOException
Why wrong: IOException is broader than needed; FileNotFoundException is more specific.