Bird
0
0

Which of the following is the correct way to declare a custom exception class named MyException in C#?

easy📝 Syntax Q12 of 15
C Sharp (C#) - Exception Handling
Which of the following is the correct way to declare a custom exception class named MyException in C#?
Aclass MyException : Exception { public MyException(string message) : base(message) {} }
Bclass MyException { public MyException(string message) {} }
Cclass MyException : int { public MyException(string message) : base(message) {} }
Dclass MyException : Exception { public void MyException(string message) {} }
Step-by-Step Solution
Solution:
  1. Step 1: Check inheritance from Exception

    Custom exceptions must inherit from Exception to behave like exceptions.
  2. Step 2: Verify constructor calls base constructor

    The constructor should call base(message) to pass the error message properly.
  3. Final Answer:

    class MyException : Exception { public MyException(string message) : base(message) {} } -> Option A
  4. Quick Check:

    Inherit Exception + call base constructor = A [OK]
Quick Trick: Inherit Exception and call base constructor for message [OK]
Common Mistakes:
MISTAKES
  • Not inheriting from Exception
  • Using wrong base class like int
  • Defining constructor as void method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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