throw vs throw e in C#

This is a classical and basic thing in C#, but I have just realized today again I was not sure about which does what. What is the difference between:

And this one:

Have you spotted the difference? The second example throws e, while the first one just re-throws the exception without the exception variable.

The first case will keep the stack trace, which is useful in most cases for debugging.

The second case will clear the stack trace and therefor hide the logic. This can be useful to hide the business logic, for example in a production global exception handling interceptor or similar code.

Send a Comment

Your email address will not be published.