Bird
0
0

Examine the following custom exception class:

medium📝 Debug Q6 of 15
C Sharp (C#) - Exception Handling
Examine the following custom exception class:
public class CustomError : Exception {
public CustomError(string message) {
Console.WriteLine(message);
}
}

What is the main issue with this implementation?
AIt lacks a parameterless constructor
BIt incorrectly uses Console.WriteLine inside the constructor
CIt should inherit from System.Exception instead of Exception
DIt does not pass the message to the base Exception class constructor
Step-by-Step Solution
Solution:
  1. Step 1: Understand Exception constructor requirements

    Custom exceptions should pass the message to the base Exception class constructor to properly initialize the exception message.
  2. Step 2: Analyze the code

    The constructor writes the message to the console but does not call base(message), so the Exception's Message property remains empty.
  3. Step 3: Evaluate other options

    It incorrectly uses Console.WriteLine inside the constructor is not an error but a bad practice. It should inherit from System.Exception instead of Exception is incorrect because Exception is the correct base class. It lacks a parameterless constructor is optional but not an error.
  4. Final Answer:

    It does not pass the message to the base Exception class constructor -> Option D
  5. Quick Check:

    Always call base(message) in custom exception constructors [OK]
Quick Trick: Call base(message) to set exception message [OK]
Common Mistakes:
MISTAKES
  • Forgetting to call base constructor with message
  • Confusing Console.WriteLine with exception handling
  • Assuming parameterless constructor is mandatory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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