Bird
0
0

You have nested try-catch blocks. In the inner catch, you want to add context info to the exception but keep original stack trace. What is the best approach?

hard🚀 Application Q9 of 15
C Sharp (C#) - Exception Handling
You have nested try-catch blocks. In the inner catch, you want to add context info to the exception but keep original stack trace. What is the best approach?
Athrow new Exception("Context info", ex);
Bthrow ex;
Cthrow;
Dthrow new Exception(ex.Message);
Step-by-Step Solution
Solution:
  1. Step 1: Understand adding context with inner exception

    Creating a new exception with the original as inner exception adds context but keeps original stack trace inside inner exception.
  2. Step 2: Compare with other options

    throw ex; resets stack trace, throw; rethrows original without added context, and throw new Exception(ex.Message); loses stack trace.
  3. Final Answer:

    throw new Exception("Context info", ex); -> Option A
  4. Quick Check:

    Add context with inner exception = C [OK]
Quick Trick: Wrap original in new exception with inner exception to add context [OK]
Common Mistakes:
MISTAKES
  • Using throw ex and losing stack trace
  • Throwing new exception without inner exception
  • Rethrowing without context

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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