Exceptions and Apologies

I’m going to talk about the art of the apology and exceptions. There is a long standing argument in software engineering about the value of exceptions versus returning errors. but we’re not going to touch that here. Instead, we are going to accept that we need to handle exceptions and how to do that better.

It all starts with the art of the apology. Several years ago, I heard a story on NPR about apologies and it sparked a lot of thought about typical, crappy public apologies. There is a format that gets repeated often by public figures which is more or less:

[x] happened and if it offended you then I’m truly sorry.

And this is a horrible apology. For the most part, it puts the onus of the apology on the receiver of the action rather than on the person committing the offense. It sounds like an apology because it has the words “I’m sorry” embedded into it, but there is no actual apology in there. With my kids, this wouldn’t cut it and even a straightforward “I’m sorry” would get a raised eyebrow from me.

Here’s what I think an apology should be:

I did [x] and it caused [y]. I’m sorry and [in the future] I will do [z].

In this case, the speaker states what they did and the consequences and goes on to say what they’re going to do to for reparation or to prevent it in the future. That’s a real apology. It includes the elements ‘what happened’, ‘what it caused’ and ‘how to make it better’.

What does this have to do with software? Easy! Exceptions are apologies! Seriously – something went wrong and we need to figure out how what happened and how to make it better.

In my current project, I have a work item which is (more or less), “make error messages delightful”. I looked at this and tried figure out how to meet that goal and the answer was simple: make it a good apology.

In my mind, and exception message should have in it what happened, how it happened (cause) and how to fix/avoid it in the future. And check it out – that matches the template of an apology! So, as writing code and you’re in a position to throw an exception and you’re about to put in “Data not found”. ask yourself if your message is a weak-sauce message or an actual apology. If you can’t include all 3 elements: what happened, how it happened, and how to make it better then you need to refactor your code so that you have all that information at that point.