Docs: Throwing an 'Exception' is not a best practice.

Created on 7 Aug 2018  Â·  5Comments  Â·  Source: dotnet/docs

from.RollbackTransaction(withdrawalTrxID);
throw new Exception("Withdrawal failed", ex);

Throwing an 'Exception' is not a best practice.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P1 up-for-grabs

All 5 comments

Who wrote that rule? You? How do you expect that inner exceptions are created in the first place? What if you have an exception handler in the attribute in your controller that logs errors or would you just create exception logging throughout the application and think that's best practice? Good luck with the next programmer not cursing you out when the exception logger is upgraded.

@crtjr64 please read carefully https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions/creating-and-throwing-exceptions#things-to-avoid-when-throwing-exceptions :)

I read @shatl 's original issue as recommending against throwing System.Exception. Is that what you meant?

@crtjr64 did you think he was recommending against throwing any exception?

@billwagner exactly

I've added this to our backlog, and applied the up-for-grabs label.

The final example should throw a more descriptive exception type, even if the full definition of the exception type is not included in this article.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LJ9999 picture LJ9999  Â·  3Comments

stanuku picture stanuku  Â·  3Comments

sebagomez picture sebagomez  Â·  3Comments

ike86 picture ike86  Â·  3Comments

svick picture svick  Â·  3Comments