2 ms·
This is a good post about error handling and exceptions. I think the title should be changed to reflect that, as I almost didn't bother reading it. I've slowly
by gruseom 18y ago
This is a good post about error handling and exceptions. I think the title should be changed to reflect that, as I almost didn't bother reading it.
I've slowly but surely gravitated away from using exceptions over the years, for different reasons than the author lays out (though I recognize some of what he's saying from my own experience).
I think the reason exceptions became popular is actually a psychological one: they are easy for the programmer. (I mean the programmer throwing the exception, not the poor sod who has to deal with it, which may be the same person of course.) You hit something you don't want to deal with? Just throw it. It's like the kids' game Hot Potato.
Ultimately, users pay the price for this evasion of responsibility, since they end up with a system that behaves erratically in cases that don't fit the happy path and is hard to understand. In the worst case, the exceptions are just thrown all the way up and the user ends up having to catch the hot potato. (Edit: you can generalize "user" here to include any programmer writing code that calls the original code.)
What I think is going on here is that programming is hard enough without error handling, and error handling makes it harder still. Good error handling models are dependent on the specifics of the program in question - they need to fit the contours of the system and of the domain itself. This is hard, so we don't want to do it, and since we can get away with deferring it, we do.
If you take a look at the author's examples, the code snippet he describes as "not bad" is the only one that attempts to handle the error in place. It's also the ugliest and by far the most complicated-looking. But the complexity is latent in all the other examples too. It's just deferred.