Bird
0
0

Identify the error in this custom exception class:

medium📝 Debug Q14 of 15
Java - Custom Exceptions
Identify the error in this custom exception class:
public class MyException extends Exception {
    public void MyException(String message) {
        super(message);
    }
}
Asuper(message) cannot be called in this class
BMissing import statement for Exception
CClass must implement Serializable interface
DConstructor has a void return type, so it's a method, not a constructor
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor syntax

    Constructors must not have a return type. Here, void makes it a method, not a constructor.
  2. Step 2: Understand consequences

    Without a proper constructor, the class uses default constructor which does not call super(message), causing errors when throwing with message.
  3. Final Answer:

    Constructor has a void return type, so it's a method, not a constructor -> Option D
  4. Quick Check:

    Constructor no return type = Constructor has a void return type, so it's a method, not a constructor [OK]
Quick Trick: Constructors never have a return type, not even void [OK]
Common Mistakes:
  • Adding void return type to constructor
  • Assuming import Exception is needed
  • Thinking super() cannot be called in subclass

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes