Bird
0
0

Which of the following is the correct constructor for a custom exception class named MyException?

easy📝 Syntax Q12 of 15
Java - Custom Exceptions
Which of the following is the correct constructor for a custom exception class named MyException?
Apublic MyException() { this.message = message; }
Bpublic void MyException(String message) { super(message); }
Cpublic MyException(String message) { super(message); }
Dpublic MyException(String message) { print(message); }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct constructor syntax

    Constructors have no return type and call super(message) to pass the message to the parent Exception class.
  2. Step 2: Check each option

    public MyException(String message) { super(message); } correctly defines a constructor calling super(message). public MyException() { this.message = message; } incorrectly assigns message without declaration. public void MyException(String message) { super(message); } has a void return type, so it's not a constructor. public MyException(String message) { print(message); } calls a non-existent method print.
  3. Final Answer:

    public MyException(String message) { super(message); } -> Option C
  4. Quick Check:

    Constructor calls super(message) = public MyException(String message) { super(message); } [OK]
Quick Trick: Constructor must call super(message) without return type [OK]
Common Mistakes:
  • Adding void return type to constructor
  • Not calling super(message) in constructor
  • Trying to assign message directly without declaration

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes