runtime: Proposal: Add static ExceptionDispatchInfo.Throw(Exception)

If you have an exception object and you want to throw it without overwriting its existing call stack and bucket information, you do:

ExceptionDispatchInfo.Capture(exception).Throw();

This is unnecessary ceremony and also incurs some additional unnecessary cost (though we’re on an exceptional path so the cost doesn’t matter as much). It’d be nicer if we could simply write:

ExceptionDispatchInfo.Throw(exception);

I propose we add the following method:

public sealed class ExceptionDispatchInfo
{
    public static void Throw(Exception source);
    ...
}

Its visible behavior would be identical to using Capture/Throw, so a valid implementation would be:

public static void Throw(Exception source) => Capture(source).Throw();

but a more efficient implementation could be provided that didn’t need to allocate an EDI, copy all of the Exception’s state into it, and then copy all of the state back.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 21 (21 by maintainers)

Most upvoted comments

An alternate shape that achieves the same purpose would be an instance or extension method on Exception, ala what @danmosemsft suggests at https://github.com/dotnet/corefx/pull/16898#issuecomment-285411090, e.g.

exc.Rethrow();