Bird
0
0

What will be the output of the following C# code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Exception Handling
What will be the output of the following C# code?
class MyException : Exception { public MyException(string message) : base(message) {} }
try {
throw new MyException("Error happened");
} catch (MyException ex) {
Console.WriteLine(ex.Message);
}
AException caught
BError happened
CMyException
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand the throw statement

    The code throws a MyException with message "Error happened".
  2. Step 2: Catch block prints exception message

    The catch block catches MyException and prints ex.Message, which is "Error happened".
  3. Final Answer:

    Error happened -> Option B
  4. Quick Check:

    Throw and catch prints message = C [OK]
Quick Trick: Catch prints exception message property [OK]
Common Mistakes:
MISTAKES
  • Expecting class name instead of message
  • Thinking catch block won't run
  • Assuming no output from exception

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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