skip to content
Khalid Abuhakmeh logo Khalid Abuhakmeh

ExceptionDispatchInfo: Capture and Rethrow Exceptions in C#

ExceptionDispatchInfo.Capture stores an exception and rethrows it later, on any thread, with its original stack trace intact, unlike throw ex.

7 min read Updated

Updated November 2026 — Verified against .NET 10. ExceptionDispatchInfo.Capture and Throw work the same as they did in .NET Framework 4.5, and .NET now also offers the static ExceptionDispatchInfo.Throw(Exception) and SetCurrentStackTrace helpers for rethrowing without holding an instance.

As long as there has been software, there have been bugs. Exceptions are a hierarchy of classes designed to help developers capture, inspect, and handle errors. For C# developers and many other languages, the way we work with exceptions is in the try/catch construct.

ExceptionDispatchInfo (in System.Runtime.ExceptionServices) captures an exception with ExceptionDispatchInfo.Capture(ex) so you can rethrow it later with .Throw(), even on another thread, without losing the original stack trace. The rethrow appends new frames after a --- End of stack trace from previous location --- marker instead of resetting the trace like throw ex; does.

In this post, we’ll look at ExceptionDispatchInfo, a unique supplement to a .NET developer’s exception handling toolbox.

What is ExceptionDispatchInfo in C#?

ExceptionDispatchInfo is a class in the System.Runtime.ExceptionServices namespace that holds an exception together with its stack trace. ExceptionDispatchInfo.Capture(ex) takes a snapshot, and Throw() rethrows the exception later, possibly on a different thread or in a different context. The original stack trace is preserved, and the frames for the rethrow location are appended to it rather than replacing it.

The Good Old Try/Catch Block

As .NET developers, we’re all familiar with the try/catch format of handling exceptions.

try {
A();
}
catch (Exception e)
{
throw;
}

When our application throws an exception from calling the method A, we know that the catch block will capture that exception. This’s a fundamental concept for all .NET developers.

There are a few caveats with this approach.

  1. We can choose to handle the exception, thus not letting the exception bubble any further.
  2. We can choose to handle specific exceptions, like NotImplementedException or ArgumentException.
  3. We can reset the stack trace by replacing throw; with throw e;. The exception then appears to originate in the catch block, and the original frames are lost.

These all have valid use cases, but what if I said that we could buffer our exception in a container designed to contain an exception?

The ExceptionDispatchInfo Class

The .NET Framework introduced the ExceptionDispatchInfo class around version 4.5.

The ExceptionDispatchInfo object stores the stack trace information and Watson information that the exception contains at the point where it is captured. The exception can be thrown at another time and possibly on another thread by calling the ExceptionDispatchInfo.Throw method. The exception is thrown as if it had flowed from the point where it was captured to the point where the Throw method is called. –Microsoft

Let’s take a look at an example.

ExceptionDispatchInfo Example

Given we have the following code, we expect our code to throw an exception of NotImplementedException at runtime. Additionally, we expect to see the methods of A, B, and C in our exception stack information.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
namespace Preview
{
class Program
{
public static void Main(string[] args)
{
try {
A();
}
catch (Exception e)
{
throw;
}
}
public static void A()
{
B();
}
private static void B()
{
C();
}
private static void C()
{
throw new NotImplementedException();
}
}
}

No surprise, we do.

Terminal window
Unhandled exception. System.NotImplementedException: The method or operation is not implemented.
at Preview.Program.C() in /Users/khalidabuhakmeh/Projects/Dotnet/Preview/Program.cs:line 34
at Preview.Program.B() in /Users/khalidabuhakmeh/Projects/Dotnet/Preview/Program.cs:line 29
at Preview.Program.A() in /Users/khalidabuhakmeh/Projects/Dotnet/Preview/Program.cs:line 24
at Preview.Program.Main(String[] args) in /Users/khalidabuhakmeh/Projects/Dotnet/Preview/Program.cs:line 14

How do we use ExceptionDispatchInfo in this scenario?

class Program
{
public static void Main(string[] args)
{
ExceptionDispatchInfo edi = null;
try {
A();
}
catch (Exception e)
{
// captured on line 15
edi = ExceptionDispatchInfo.Capture(e);
}
// throw again on line 24
edi.Throw();
}
public static void A()
{
B();
}
private static void B()
{
C();
}
private static void C()
{
throw new NotImplementedException();
}
}

What’s surprising is that we see the original stack trace and the line where we throw the exception.

Terminal window
Unhandled exception. System.NotImplementedException: The method or operation is not implemented.
at Preview.Program.C() in /Users/khalidabuhakmeh/Projects/Dotnet/Preview/Program.cs:line 39
at Preview.Program.B() in /Users/khalidabuhakmeh/Projects/Dotnet/Preview/Program.cs:line 34
at Preview.Program.A() in /Users/khalidabuhakmeh/Projects/Dotnet/Preview/Program.cs:line 29
at Preview.Program.Main(String[] args) in /Users/khalidabuhakmeh/Projects/Dotnet/Preview/Program.cs:line 15
--- End of stack trace from previous location ---
at Preview.Program.Main(String[] args) in /Users/khalidabuhakmeh/Projects/Dotnet/Preview/Program.cs:line 24

How Do I Use ExceptionDispatchInfo.Capture?

Call Capture inside a catch block, keep the returned object, and call Throw() wherever you want the exception to resurface. Here is a minimal version:

using System.Runtime.ExceptionServices;
class P
{
static void C() => throw new NotImplementedException();
static void B() => C();
static void A() => B();
static void Main()
{
ExceptionDispatchInfo? edi = null;
try { A(); }
catch (Exception ex) { edi = ExceptionDispatchInfo.Capture(ex); }
// ...clean up, switch threads, run other work...
try { Rethrow(edi!); }
catch (Exception e) { Console.WriteLine(e); }
}
static void Rethrow(ExceptionDispatchInfo edi) => edi.Throw();
}
Terminal window
System.NotImplementedException: The method or operation is not implemented.
at P.C()
at P.B()
at P.A()
at P.Main()
--- End of stack trace from previous location ---
at P.Rethrow(ExceptionDispatchInfo edi)
at P.Main()

The first four frames are the original throw. Everything after the marker is where Throw() was called.

throw vs throw ex vs ExceptionDispatchInfo

Approach Stack trace result Use it when
throw; Original trace preserved, nothing appended You rethrow immediately from the catch block
throw ex; Trace reset to the throw ex; line, original frames lost Almost never. It hides where the error came from
ExceptionDispatchInfo.Capture(ex).Throw() Original trace preserved, rethrow frames appended after the marker You rethrow later, from another method, thread, or outside the catch block
ExceptionDispatchInfo.Throw(ex) (static, .NET Core 2.0+) Same as above, without keeping an instance You rethrow right away but can’t use throw;, such as inside a helper method

On .NET 10, throw ex; produces a trace containing only the frame that contained the throw ex; statement, while throw;, Capture(ex).Throw(), and the static Throw(ex) all keep C, B, and A.

ExceptionDispatchInfo also has two helpers for exceptions that haven’t been thrown yet. SetRemoteStackTrace(ex, string) (.NET 6+) stores a stack trace you supply (for example, one received from another service), which then prints ahead of the marker when you throw. SetCurrentStackTrace(ex) (.NET 5+) fills in the current call stack on an exception you created but didn’t throw, useful when you return exceptions as values.

How Does await Use ExceptionDispatchInfo?

When an awaited task faults, the continuation rethrows the stored exception using ExceptionDispatchInfo internally. That is why await gives you the original stack frames from the failing method rather than a trace that starts at the await line. You get this behavior without writing any ExceptionDispatchInfo code yourself.

FAQ

What namespace is ExceptionDispatchInfo in?

System.Runtime.ExceptionServices. Add using System.Runtime.ExceptionServices; to use it.

Does ExceptionDispatchInfo preserve the stack trace?

Yes. Throw() keeps the captured frames and appends the rethrow location after --- End of stack trace from previous location ---.

What is the difference between throw and throw ex?

throw; rethrows the current exception and keeps its stack trace. throw ex; resets the stack trace to the line where you wrote it. See C# try/catch exception handling for the basics.

Can I rethrow an exception on a different thread?

Yes. Capture it on the thread where it happened, pass the ExceptionDispatchInfo across, and call Throw() on the target thread. This is the main reason the class exists.

Is the static ExceptionDispatchInfo.Throw available in .NET 10?

Yes. ExceptionDispatchInfo.Throw(Exception) has been available since .NET Core 2.0, so you can use it without keeping a captured instance.

Conclusion

Awesome! ExceptionDispatchInfo allows us to store an exception for a period within our AppDomain (the class is not serializable). This storage mechanism can help us to perform cleanup tasks or other necessary steps before throwing an exception. As long as it’s within the same AppDomain, this class can help us transport and throw an exception. The transport approach can be useful in programming models that have background threads and a main thread.

If you’ve used ExceptionDispatchInfo in your codebase, I’d love to hear how and where you found it useful.

Comments

Related Posts