Bird
0
0

What will happen if a custom checked exception is thrown but not declared or caught?

medium📝 Predict Output Q5 of 15
Java - Custom Exceptions
What will happen if a custom checked exception is thrown but not declared or caught?
class MyException extends Exception {}

public class Test {
  public static void main(String[] args) {
    throw new MyException();
  }
}
AProgram compiles and runs without errors
BCompilation error due to unhandled checked exception
CRuntime exception occurs
DProgram prints an error message
Step-by-Step Solution
Solution:
  1. Step 1: Identify exception type

    MyException extends Exception, so it is a checked exception.
  2. Step 2: Check handling requirements

    Checked exceptions must be declared or caught; otherwise, compilation fails.
  3. Final Answer:

    Compilation error due to unhandled checked exception -> Option B
  4. Quick Check:

    Checked exceptions must be declared or caught [OK]
Quick Trick: Checked exceptions must be declared or caught [OK]
Common Mistakes:
  • Thinking unchecked exceptions require declaration
  • Assuming runtime error instead of compile error
  • Believing program runs without handling checked exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes