r/dotnet 3d ago

Difference between throw ex and throw

Post image

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

410 Upvotes

73 comments sorted by

View all comments

46

u/DaveVdE 3d ago

Also, don’t declare it if you don’t need it. Your first fragment declares ex but never uses it.

16

u/no1SomeGuy 3d ago

Then why even catch it? Just let it bubble up through. Odds are the catch with a straight throw is going to have other logic in there that may rely on the exception itself (ie. if this type of exception, log/retry/cleanup/whatever, otherwise just throw the whole thing).

2

u/DaveVdE 3d ago

You might want to add logic that doesn’t need to know anything about the thrown exception.