Error Messages – How to Assign Error Codes

error messages

How, when developing a medium sized project, do you identify, create and maintain error codes?

I for the life of me can't think of a simple and clean method of doing so. Some of my ideas convert class names and method name into an integer string, but that is way to long to display to the user on top of the fact that method names and class names may change (hopefully not!). Others are just using an incrementing log system (ie. when ever I create a new error message, just add 1 to the last error message id). But that is just completely unorganized.

To be more specific I am talking about error code such as:

Error 401 Unauthorized.

Best Answer

No.

Error codes are an anachronism, they stem from ye olden days when output was really hard and expensive, and the only way to signal an error condition may have been through a bunch of front panel lights: pdp11/70 front panel

These days, we have mature exception handling built into pretty much every mainstream language. Use it. Give the user information they can work with; don't bother them with technical blah-blah, but rather tell them roughly what went wrong and what they can do about it. For logging, just give your exceptions descriptive names, and log the name. Easier to remember, and also easier to find using grep or similar search tools.

The exception is, of course, when you're programming for situations where output is still hard and expensive, such as embedded systems or network protocols. HTTP still uses numeric response codes because they are extremely easy to parse efficiently - in some situations, reading just the first digit can tell you enough already, and you can discard the rest of the packet.

Related Topic