Wednesday, June 27, 2012

Alway give extremely verbose error information

When programs encounter errors, they should always make available detailed information about what actually went wrong. If it's too verbose, or too technical for the expected audience, it should still be made available; either behind a "Details" button, or written in a log file.

An anti-example is the when the awesome tool, ReSharper, runs an NUnit test, it can first automatically build any changed projects. (Nice.) But when that build fails, it says "Build failed". That's it. No output in the build output or errors windows. No nothing.

(I found this nice blog post explaining what the problem was.)

A corollary of this philosophy is that when catching an exception and throwing a different exception, you should always nest/wrap/chain the caught exception as the inner exception of the new one. The outer exception might repeat information from the inner exception; include the inner exception anyway. The inner exception may be verbose, confusing, etc.; include it anyway.

(The only, rare "exception" to this rule is when the inner exception reveals sensitive information -- credentials, HIPPA info, etc. This is usually code running on a server, in which case the inner exception should instead be logged into a secure log file for possible later problem analysis.)

No comments:

Post a Comment