Dealing with Exceptions, Logging and Displaying Error Messages
I have a client who is very firm on the idea that the user should know what went wrong when exceptions are thrown. So we're displaying error messages like:
“The database did not respond”
“The database operation failed”
“Failed to load configuration setting X”
If it were just me, I would simply just print something like:
“Oops, something unexpected happened. Please try again or contact the support department”
Let’s assume that we show the user a more detailed error message. What is he/she supposed to do with it? Remember, it’s from an exception. If something exceptional happens it unlikely that the user can fix the problem by reading an error message. At tops the user can try do perform the operation again.
When the user have got tired of trying he/she calls the support department. If we’re lucky the user manages to quote the error message exactly. But really. Do that help us? Not really. We still have to go through our logs to find out what happened.
The correct approach to dealing with exceptions
So what should we do when an exception happens? Well. Try to prevent it from happening in the future. That goes for all exceptions. Some can be prevented, whilst others can’t. The point is that we should at least try.
To be able to try to fix the exceptions we have to find them. Using a big fat log for everything is not a very good way. Instead try to have a seperate log for all unhandled exceptions. Make sure that you read that log. You might want to build something that emails that log to you daily.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)