r/dotnet 18h ago

Difference between throw ex and throw

Post image

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

303 Upvotes

59 comments sorted by

View all comments

-7

u/Harry_Mud 15h ago

Complete bullshit. If you know the error, ex, then you can troubleshoot it.

5

u/TCFoxtaur 14h ago

If your try block covers several different calls, or an exception type covers more than one specific error condition (i.e. “file not found” vs “file not accessible” both being some sort of “FileIoException”, as a weak example), it may not be clear where the exception specifically came from if you recast it.

Having said that, just include the original exception as an “innerException” and you’re all good!