Tuesday, December 02, 2008

Exception-throwing antipatterns

Tim McCune has written an interesting article called Exception-Handling Antipatterns, at http://today.java.net (a fine place to find articles of this kind, BTW). The comments at the end of the article are every bit as stimulating as the article itself.

McCune lists a number of patterns that (I find) are very widely used (nearly universal, in fact) in Java programming, such as log-and-rethrow, catch-and-ignore, and catch-and-return-null; all considered evil by McCune. My comment is: If those are antipatterns, the mere fact that such idioms are so ubiquitous in real-world Java code says more about the language than it does about programmers.

I've always had a love-hate relationship with the exception mechanism. On the whole, I think it is overused and overrated, at least in the Java world, where people seem to get a little nutty about inventing (and sublcassing) custom exceptions and ways to handle them, when they should probably spend that energy writing better code to begin with.


  1. Anonymous5:18 PM

    I agree that there are lots of exception-handling anti-patterns. Most of the problems arise from not simply throwing exceptions upward until you reach a method that can dispose of them properly (the root of all three anti-patterns you cite).

    But these anti-patterns are hardly universal. I know lots of Java programmers who'd never dream of catching and ignoring an exception.

    So please don't urge Sun to throw the baby out with the bath water because of under-educated programmers.

    What should a socket-based input stream do if there's a network error during a read? There can't be a test/act pattern, as it won't be guaranteed to hold between one call and the next. We could add a new integer magic-number return code (-1 means end of stream, so how about -2 for a network exception?).

    That's what you'd do in C (or at least it's what the last company I worked for did, for whom I had the pleasure of integrating Brendan Eich's SpiderMonkey implementation of JavaScript into a speech recognizer's semantic parser). Then you hack up the equivalent of Java's finally blocks to make sure you free up resources like memory or file handles. These cleanup blocks typically have multiple points of entry, so you either deal with lots of gotos or incredibly crufty if/then blocks in the code.

  2. Make sure to click here because this blog has some interesting info. This could be interesting experience

  3. Great post i really enjoyed this, you can also get some good information through this blog rgb vs cmyk

  4. Wonderful blog, you share some informative post, it's really helpful for me, our company https://logonick.com/ is highly appreciate your work.

  5. Amazing blog, i appreciate the work you do great job like this https://logoliberty.com/

  6. This is my first blog visit and through this i get too much valuable information like this https://maestrologodesign.com.au/

  7. I really appreciate your hard work for bringing this useful article thank you for sharing this useful information.
    https://Todo Noticias For PC

  8. I wanted to inform you that you have people like me who appreciate your work. Definitely a great post I would like to read this article.


  9. Packers and Movers Hyderabad are ✔✔✔ Certified Service Providers for Affordable Household Shifting. ***Get Instant Quotes and Compare ###Save Time- @ Packers And Movers Hyderabad

  10. Best Tips to Improve Your Essays
    While writing an essay, it's essential to consider the angle you're taking and avoid topics that have been discussed a hundred times. This way, you'll leave better points unspoken. Remember, tutors read a lot of essays every term, and you must capture their attention from the start. Use these tips to improve your essays and increase your chances of receiving top marks.

    Storyboarding is an EssayMap to present ideas and convey them in a more engaging and concise manner. The process can be done using a variety of tools, including desktop applications and online tools. Depending on the task, the storyboard can be made up of several panels, each depicting a scene and providing technical details. Bulleted lists can also make it easier to follow the steps of a storyboard.


Add a comment. Registration required because trolls.