How to rethrow InnnerException without losing Stack Trace in C# ?

Published on February 9, 2016 by abundantcode

Filed under C#

Last modified February 9, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 4309 times

Microsoft .NET Framework 4.5  introduced the ExceptionDispatchInfo class which lets the developers to capture an exception and rethrow it without losing the stack trace.

How to rethrow InnnerException without losing Stack Trace in C# ?

Below is a sample code snippet demonstrating the usage of this class.

try
{
    // Execute method
}
catch(Exception ex)
{
    ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
}

Leave a Comment


x

You might be Interested in these Posts

How to Get Necessary Information from the Exception in C# ?

There are times when you want to get the necessary information from the exception to identify the er...

How to Rethrow an Exception in C# ?

There are times when you want to handle the exception and then pass it to the higher level . Below i...

How to decode a base64 string in C#?

Here's a sample code snippet demonstrating how you can easily decode a base64 encoded string in C#. ...