Bird
Raised Fist0

What is wrong with this exception handling code?

medium📝 Debug Q7 of Q15
C Sharp (C#) - Exception Handling
What is wrong with this exception handling code?
try {
  // code
} catch (Exception ex) {
  Console.WriteLine("Error: " + ex.Message);
  throw new Exception("New error");
}
AIt swallows the exception silently
BIt correctly rethrows the original exception
CIt loses original exception details by throwing a new exception
DIt causes a syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand throwing new exception

    Throwing new Exception("New error") replaces the original exception with a new one.
  2. Step 2: Effect on debugging

    This loses the original exception's stack trace and message, making debugging harder.
  3. Final Answer:

    It loses original exception details by throwing a new exception -> Option C
  4. Quick Check:

    Throwing new exception loses original info = D [OK]
Quick Trick: Avoid throwing new exceptions inside catch unless wrapping carefully [OK]
Common Mistakes:
MISTAKES
  • Assuming new exception preserves original stack trace
  • Expecting syntax errors
  • Thinking exception is swallowed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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