Chapter 28. Don’t Nail Your Program into the Upright Position

Verity Stob

image with no caption

I ONCE WROTE A SPOOF C++ QUIZ, in which I satirically suggested the following strategy for exception handling:

By dint of plentiful try…catch constructs throughout our codebase, we are sometimes able to prevent our applications from aborting. We think of the resultant state as "nailing the corpse in the upright position.”

Despite my levity, I was actually summarizing a lesson I received at the knee of Dame Bitter Experience herself.

It was a base application class in our own, homemade C++ library. It had suffered the pokings of many programmers’ fingers over the years: nobody’s hands were clean. It contained code to deal with all escaped exceptions from everything else. Taking our lead from Yossarian in Catch-22, we decided, or rather felt (decided implies more thought than went into the construction of this monster) that an instance of this class should live forever or die in the attempt.

To this end, we intertwined multiple exception handlers. We mixed in Windows’ structured exception handling with the native kind (remember __try…__except in C++? Me, neither). When things threw unexpectedly, we tried calling them again, pressing the parameters harder. Looking back, I like to think that when writing an inner try…catch handler within the catch clause of another, some sort of awareness crept over ...

Get 97 Things Every Programmer Should Know now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.