Re:Isaacs: try/catch is an anti-pattern
by viraptor on Monday November 14, 2011 @10:50AM
(#3234398)
I'm sorry, but this is a very bad example. iptables had an issue for a long time where error code is not carefully preserved in many situations and you end up with messages like:
This wouldn't happen with exceptions - even if not handled properly, you'd see where is it originating and what's the most probable cause of the issue. And it's not necessarily iptable's fault - in some cases you have to really bend some rules to get the error you want. Also you cannot stack them so if you fail to cleanup after the original error, what do you return? The first or the second error? One has to be ignored.
[ Parent
| on HN ]
Return the first one. You always care more about the original error than an explanation of why you failed in cleaning up after it.
Counterexample: you failed parsing some number correctly, but the backend where you log broken transactions is corrupted and cannot be written to. You'd rather drop the second information?
Ah, so you're saying that failing to parse the number breaks the transaction.
Fair enough, you'd rather (as a human being) have the second one. As a matter of building the system, I would still return the first one.
Basically, the second information is important in the sense that you really want an audit trail, but unimportant in that it doesn't tell you why your call failed. Returning it to the caller is useless compared to returning the first error.
But yes, it should certainly be logged and potentially acted on. An application can't do anything useful with the information, but an ops guy can.
So return the first one.