Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of throwing an exception?

I'm writing a login class in c#, and I'm trying to be diligent about throwing exceptions for null passwords, insufficiant password characters, etc. The thing that suddenly occured to me was - What do I do with these exception? What/who are they for? Whether I handle the exception or not, the app will still fail at that point. Is the exception for other developers, the customer!?

like image 292
hoakey Avatar asked Dec 04 '22 10:12

hoakey


1 Answers

Exceptions are used to provide detailed information about the cause of a particular failure. If you simply let the code fail on its own you miss the opportunity to provide richer details about the actual cause of the failure.

Your users should not be seeing the information you add to your exceptions. Instead, consider adding a global exception handler that catches your detailed exceptions, logs the information, and then displays a friendly error message to your user.

Exceptions provide not only a detailed message in your logs about why the failure occurred (i.e. password was null in your example) but also call stack information that indicates the call chain that led to the exception. In a login form this is less important. However, in a multi-threaded asynchronous client/server application this can be critical.

This article contains many good guidelines: http://msdn.microsoft.com/en-us/library/ms229005.aspx

like image 129
Stuart Thompson Avatar answered Dec 21 '22 09:12

Stuart Thompson