Error Handling with Exceptions: Error Code Fail

By | October 26, 2010

This article is part of a longer running series on Error Handling with Exceptions.

Right around 2000, I began to explore error handling via exceptions. There were many things I liked about exceptions; as a designer, I could:

  • Ensure that users processed error conditions
  • Provide rich contextual information along with the error itself

Unfortunately, I wasn’t at all comfortable with the introduction of jump/goto-like semantics when exceptions were introduced. In my naive-ate, I decided that I was going to create a solution that leveraged the best of both words.

I made good progress as I attempted to create an error code-based alternative to exceptions. Despite this progress I quickly realized that I was fighting a losing battle. The code that I was producing, while correct, was cumbersome to use – if I was having a hard time using it how could I expect success while evangelizing its use. Complexity resulted from the requirements that return codes must be composable and be convertible to a function’s result type. Worse, I realized that the end result of this effort was precluding the use of something that I had previously grown to love – the Standard Template Library. Finally, the killer was that the C++ new operator throws std::bad_alloc instead of returning null upon failure. By moving forward, I was swimming against the current of the entire C++ community and ultimately destined for failure.

It became overwhelmingly clear that it wasn’t possible, practical, or even prudent to try to fight the use of exceptions any longer. With this in mind, my focus changed to the evaluation and understanding of exceptions. This led to ultimately embracing exceptions as a superior means of error communication and propagation. I can honestly say that this change in perspective has had a profoundly positive impact on the designs that I produce and the quality of the code that I write. After literally hundreds of hours of thought, investigation, and experimentation, I hope to share some of what I learned along that journey.

In the coming days/weeks, I hope to write on the following topics. Please feel free to post in the comments if there is anything that you would like to see in these blog entries or specific questions that you may have on the topic.

Fully understanding and embracing exceptions is hard work, but my experience shows that the rewards greatly outweigh the cost.

Leave Your Comment

Your email will not be published or shared. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Please wrap all source codes with [code][/code] tags. Powered by