Bird
0
0

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

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Exception Handling
What will be the output of the following C# code?
try {
  throw new Exception("Error");
} catch (Exception ex) {
  Console.WriteLine(ex.StackTrace == null ? "No stack trace" : "Has stack trace");
  throw;
}
ANo stack trace
BCompilation error
CHas stack trace
DRuntime error without message
Step-by-Step Solution
Solution:
  1. Step 1: Analyze exception throwing and catching

    The code throws an exception, catches it, and prints whether the stack trace is null or not.
  2. Step 2: Understand stack trace presence

    When an exception is thrown, it has a stack trace. Inside the catch block, ex.StackTrace is not null, so "Has stack trace" is printed.
  3. Final Answer:

    Has stack trace -> Option C
  4. Quick Check:

    Stack trace presence = B [OK]
Quick Trick: Exception stack trace is set on throw, not null inside catch [OK]
Common Mistakes:
MISTAKES
  • Assuming stack trace is null inside catch
  • Confusing output with error messages
  • Expecting compilation errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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