Bird
0
0

Which of the following correctly defines a custom exception class named ConnectionFailedException in C#?

easy📝 Syntax Q3 of 15
C Sharp (C#) - Exception Handling
Which of the following correctly defines a custom exception class named ConnectionFailedException in C#?
Apublic class ConnectionFailedException : Exception { public ConnectionFailedException(string message) : base(message) {} }
Bpublic class ConnectionFailedException { public ConnectionFailedException(string message) { this.message = message; } }
Cclass ConnectionFailedException : Exception { public void ConnectionFailedException(string msg) { base(msg); } }
Dpublic class ConnectionFailedException : Exception { public ConnectionFailedException() { base(); } }
Step-by-Step Solution
Solution:
  1. Step 1: Check inheritance

    The custom exception must inherit from Exception.
  2. Step 2: Check constructor syntax

    The constructor should call the base class constructor with the message parameter.
  3. Step 3: Evaluate options

    public class ConnectionFailedException : Exception { public ConnectionFailedException(string message) : base(message) {} } correctly inherits and calls base constructor. public class ConnectionFailedException { public ConnectionFailedException(string message) { this.message = message; } } lacks inheritance and uses invalid message assignment. class ConnectionFailedException : Exception { public void ConnectionFailedException(string msg) { base(msg); } } uses a method instead of a constructor and incorrectly calls base. public class ConnectionFailedException : Exception { public ConnectionFailedException() { base(); } } calls base parameterless constructor but lacks message parameter.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Custom exceptions inherit Exception and call base(message) [OK]
Quick Trick: Inherit Exception and call base(message) constructor [OK]
Common Mistakes:
MISTAKES
  • Not inheriting from Exception
  • Not calling base constructor with message
  • Using method instead of constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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