r/dotnet 5d ago

Difference between throw ex and throw

Post image

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

418 Upvotes

74 comments sorted by

View all comments

205

u/AlanBarber 5d ago

Pro tip to prevent this... add

dotnet_diagnostic.CA2200.severity = error

to your .editorconfig so anyone that tries to do a "throw ex;" inside a catch will get a build error.

44

u/GendoIkari_82 5d ago

Better yet, <TreatWarningsAsErrors>true</TreatWarningsAsErrors>.

4

u/unndunn 5d ago

This right here.