Bird
0
0

What is the problem with the following Java method?

medium📝 Debug Q6 of 15
Java - Exception Handling

What is the problem with the following Java method?

public void readFile() {
    throw new FileNotFoundException("File missing");
}
AYou cannot throw exceptions inside methods.
BThe method does not declare the checked exception with <code>throws</code>.
CFileNotFoundException cannot be instantiated.
DThe exception message is invalid.
Step-by-Step Solution
Solution:
  1. Step 1: Recognize checked exceptions

    FileNotFoundException is a checked exception and must be declared or handled.
  2. Step 2: Check method declaration

    The method does not declare throws FileNotFoundException, so compilation error occurs.
  3. Final Answer:

    The method does not declare the checked exception with throws. -> Option B
  4. Quick Check:

    Checked exceptions require declaration or try-catch [OK]
Quick Trick: Checked exceptions must be declared or caught [OK]
Common Mistakes:
  • Ignoring checked exception declaration
  • Assuming all exceptions are unchecked
  • Not using try-catch or throws for checked exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes