Joel rails against exceptions in his latest blog entry which I find both surprising and disappointing. Normally, Joel has a lot of good insights but this post is way off the mark in my opinion. Exception handling has been added to most modern languages because the old approach - advocated by Joel - of returning error codes from functions creates ugly code and makes it hard to follow the 'successful' flow of the application. If a function has to return success / failure, then it can't also return a result value unless you either return a structured result (containing the success / failure code and the result - if successful) or you must always pass a result variable by reference as a function argument (something not even possible in every language). The trouble with error code returns is that nothing forces you to check them - you can always go straight to the result variable (because it "can't possibly fail in this case"). Your code ends up totally overrun with code to check error code returns and you can't see the straight line code any more...
By contrast, exception handling lets you cleanly abstract out the error processing. You can write simple, neat code that deals with the straight line case and bookend it with any necessary error handling (using try/catch). You don't even need to be aware of some error conditions - you can leave them for your caller to handle because your code just simply might not be able to do anything useful with the error anyway. Exception handling reduces coupling between components and lets you improve cohesion and reuse - because error handling doesn't have to be inherent to every single component you write.
Joel On Exceptions : Just Say No
October 13, 2003 · 10 Comments
Tags: programming

10 responses so far ↓
1 Andreas Ryge // Sep 8, 2004 at 5:04 PM
2 Dominick // Sep 8, 2004 at 5:04 PM
3 Christian Cantrell // Sep 8, 2004 at 5:04 PM
4 Hefin Jones // Sep 8, 2004 at 5:04 PM
5 Scott Keene // Sep 8, 2004 at 5:04 PM
6 Steven Johnson // Sep 8, 2004 at 5:04 PM
7 Kwang // Sep 8, 2004 at 5:04 PM
8 Jason Clark // Sep 8, 2004 at 5:04 PM
9 seancorfield // Sep 8, 2004 at 5:04 PM
10 Jon Hart // Sep 8, 2004 at 5:04 PM
Leave a Comment