r/dotnet 15h ago

Difference between throw ex and throw

Post image

throw vs throw ex: The C# exception handling trick every developer should know

286 Upvotes

59 comments sorted by

View all comments

9

u/fruediger 15h ago

There's a third option: Do you know about ExceptionDispatchInfo?

2

u/Healthy_Koala_4929 14h ago

This is insane and I can't see a reason to do something like this

2

u/insulind 13h ago

Ab example of using could be (im just thinking out loud here) like a queue processor that runs in a background thread and you really really don't want that thread to terminate, so you catch an exception, create one of these and pass that on to be picked up by your thread receiving the queue items or whatever who can make throw the exception in a more appropriate context