Skip to content

System.Net: Replace existing "catch Exception / throw later" patterns with ExceptionDispatchInfo  #16191

Closed
dotnet/corefx
#16898
@CIPop

Description

@CIPop

@stephentoub :

There are a bunch of places in the code where an Exception is caught and then later thrown. This kind of re-throwing ends up clobbering the original call stack and Watson bucket stored in the Exception, making it more difficult for someone looking at the exception to understand where it came from. To address this, ExceptionDispatchInfo was added in .NET 4.5, and is now used in places like Task that have this exact issue. I'd suggest we look at using ExceptionDispatchInfo in all of these places. Basically, instead of just storing an Exception, you'd store the result of ExceptionDispatchInfo.Capture(caughtException), and then instead of using throw _caughtException;, you'd do _caughtExceptionDispatchInfo.Throw();. ExceptionDispatchInfo will preserve the original information and append the new throw location's call stack rather than using it to overwrite the original.

This was discussed in dotnet/corefx#5541 (comment).

Metadata

Metadata

Assignees

Labels

area-System.NetenhancementProduct code improvement that does NOT require public API changes/additions

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions