Bird
Raised Fist0

What is wrong with this custom exception class?

medium📝 Debug Q7 of Q15
C Sharp (C#) - Exception Handling
What is wrong with this custom exception class?
public class InvalidDataException : Exception {
public InvalidDataException(string message) {
Message = message;
}
}
AClass must be abstract
BMissing override keyword
CCannot assign to Message property directly
DConstructor must be private
Step-by-Step Solution
Solution:
  1. Step 1: Check assignment to Message property

    Message is a read-only property in Exception, so it cannot be assigned directly.
  2. Step 2: Correct way to set message

    The message should be passed to the base constructor using : base(message).
  3. Final Answer:

    Cannot assign to Message property directly -> Option C
  4. Quick Check:

    Set message via base constructor, not property assignment [OK]
Quick Trick: Pass message to base constructor, don't assign Message property [OK]
Common Mistakes:
MISTAKES
  • Assigning Message property directly
  • Forgetting base constructor call
  • Misunderstanding property access

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes