My musings about .NET and what not

The Greatest Exception Handling WTF?!? of All Time

The Exception object has one purpose and one purpose only – to represent a runtime error, nothing more. Exceptions should never be used for purposes for which they were not intended, or you could end up with this monstrosity.

Take a close look at the following code. This is supposed to be a helper method that determines whether an integer that is passed to it is even or odd.

public static class NumberHelpers
   public static ApplicationException EvenOrOdd(int integer)
      if (integer % 2 == 0)
         return new ApplicationException(“The integer is even.”);
         return new ApplicationException(“The integer is odd.”);

Hold on, it gets even better. Below is a usage example.

protected void btnTest_Click(object sender, EventArgs e)
      throw NumberHelpers.EvenOrOdd(Convert.ToInt32(txtIntToTest.Text));
   catch (ApplicationException ex)
      litResult.Text = ex.Message;

I’ve seen some pretty heavy exception abuse in my time, but I do believe this one wins the cigar.

In the first listing, you can see that the EvenOrOdd method uses an exception as a return type. While this is acceptable in helper methods that create and initialize real exceptions for use elsewhere in your code, that’s definitely not the case here.

Don’t use an exception as a return type or an out parameter for a method.

In the second listing, the page code that uses the method actually requires the the returned exception to be thrown in order to fulfill its normal function.

Exceptions should only be thrown in response to a runtime error. Exceptions should never be used to direct the normal flow of execution in a program.

The scary thing is that this code actually works! Even so, you wouldn’t be caught dead writing code like this… right?

Care to share your favorite Exception Handling WTF?!? Leave a comment and let us all in on the fun.


Subscribe to this blog for more cool content like this!

kick it on

shout it on

vote it on

Bookmark / Share

    » Similar Posts

    1. Defensive Programming, or Why Exception Handling Is Like Car Insurance
    2. Should We Return Null From Our Methods?
    3. If At First You Don’t Succeed - Retrying Mail Operations in .NET

    » Trackbacks & Pingbacks

    1. You've been kicked (a good thing) - Trackback from

      The Greatest Exception Handling WTF?!? of All Time — May 27, 2009 1:40 PM
    2. Pingback from DevLanfear - .NET

      DevLanfear - .NET — May 28, 2009 9:15 AM
    3. Pingback from links for 2009-05-28 | Xelluloid

      links for 2009-05-28 | Xelluloid — May 28, 2009 8:02 PM
    4. Pingback from links for 2009-05-28 | Xelluloid

      links for 2009-05-28 | Xelluloid — May 28, 2009 8:02 PM
    5. Thank you for submitting this cool story - Trackback from DotNetShoutout

      The Greatest Exception Handling WTF?!? of All Time — May 28, 2009 9:09 PM
    6. Pingback from MsHelp » Week 22: Things I need to check out

      MsHelp » Week 22: Things I need to check out — May 29, 2009 2:07 PM
    7. Pingback from Exceptions are for Exceptions

      Exceptions are for Exceptions — May 29, 2009 2:35 PM
    8. Pick of the week: The Holy Grail is NOT Automation General .NET 4 Cancellation Framework : A post by the Parallel FX team about how you’ll be able to build cancellation-aware applications in .NET 4.0. Why Defer Loading in Entity Framework Isn’t Going

      Weekly Web Nuggets #66 — June 2, 2009 1:52 PM
    9. Pingback from The Rules of the Game « AJ’s blog

      The Rules of the Game « AJ’s blog — July 19, 2009 8:03 AM
    Trackback link for this post:

    » Comments

    1. Jay Cincotta avatar

      That's hysterical. But, it makes you wonder how that code came to be. There's got to be a story lurking beneath this.

      And, of course, the code doesn't handle the legitimate exception raised if txtIntToTest.Text is non-numeric.

      Jay Cincotta — May 27, 2009 3:18 PM
    2. Lee Dumond avatar

      >> And, of course, the code doesn't handle the legitimate exception raised if txtIntToTest.Text is non-numeric

      @Jay -- let's hope for the sake of all mankind he was using input validation for that. ;)

      Lee Dumond — May 27, 2009 6:01 PM
    3. Justin Etheredge avatar

      Ha, that is classic!

      Justin Etheredge — May 28, 2009 7:37 AM
    4. Brent Schooley avatar

      Nothing short of a really really bad way. Hilarious.

      Brent Schooley — May 28, 2009 8:43 AM
    5. Jef Claes avatar

      I'm stunned :| Exceptions are for exceptions! :P

      Jef Claes — May 28, 2009 9:19 AM
    6. Shay Friedman avatar

      Oh my!

      That's what they mean when they say "there were signs for what's going to happen" after they catch a serial killer :)

      Shay Friedman — May 28, 2009 9:26 AM
    7. Steve Calvert avatar

      Can you say, "code review"?

      Steve Calvert — May 28, 2009 10:27 AM
    8. Bob avatar

      Code looks good to me. After all the code copes nicely with the situation where conversion from string to int throws and exception.

      Bob — May 28, 2009 11:00 AM
    9. Jason Simone avatar

      Perhaps Bob is joking. I'm not usually one to harp on slightly extraneous use of exceptions, but this one is just silly.

      And, I'm not going to bother testing it, but wouldn't this actually break in run-time if Convert.ToInt32 threw a cast exception?

      Jason Simone — May 28, 2009 11:54 AM
    10. Adam avatar

      While this example definitely is the worst that I've seen I work with a real piece of work (that's the nicest thing I could think to say), who wraps every line of his code around empty Try/Catch blocks!!! He's the "lead dev" too, and I've had little success getting it through his thick skull why this is wrong. And that is just one reason why I'm in the process of finding another job :).

      Adam — May 28, 2009 12:04 PM
    11. dave avatar

      It's obviously a joke. Laugh.

      dave — May 28, 2009 12:08 PM
    12. Lee Dumond avatar

      @Adam - There was a person who made a similar comment to this recent post of mine:

      Might you both be talking about the same gentleman? ;)

      Lee Dumond — May 28, 2009 12:08 PM
    13. Erik avatar

      Actually this code will still fail at runtime if the string passed in cannot be parsed - the catch block is explicitly catching ApplicationException.

      Either way, those who write code like this should be dragged out into the street and shot full of proper programming techniques.

      Erik — May 28, 2009 12:10 PM
    14. Paul avatar


      I really love how the method returns the "exception" and the caller is throwing the returned Exception to catch it and process the result - WTF.

      I agree, probably the worst case of exception abuse I've seen.

      Paul — May 28, 2009 3:29 PM
    15. Huh avatar

      @Adam - Hmm I knew a Lead Dev who used to wrap his code with a try and a catch block containing just a "throw". His explanation was that this helped him in debugging exceptions by allowing him to put a breakpoint on the "throw" :)

      Of course the real problem was that the application threw all sorts of unnecessary exceptions which were caught and ignored. So using the Debug->Exceptions dialog actually made it harder to just chase the exception he wanted. And there isn't a way to just select custom application exceptions in that dialog.

      But... it was still funny to see a Lead Dev write code like that :)

      Huh — May 28, 2009 4:06 PM
    16. luke avatar

      oh god

      luke — May 28, 2009 7:54 PM
    17. Veera avatar

      holy cow!!

      Veera — May 29, 2009 5:05 AM
    18. Veera avatar

      holy cow!

      Veera — May 29, 2009 5:05 AM
    19. John Rockefeller avatar

      Hey if it works, who cares?



      Just kidding! :D

      John Rockefeller — May 29, 2009 8:15 AM
    20. HB avatar

      That is some 'exceptional' code to say the least...

      HB — May 29, 2009 11:29 AM
    21. Juan avatar

      man, that code must be a joke, I cannot believe someone can put that in there seriously.

      Juan — June 2, 2009 8:39 PM
    22. Hatim avatar

      Hey how did you get my code!!!! :D

      Hatim — June 3, 2009 9:50 AM
    23. Timm avatar

      That's crazy! Most programmers know that throwing exceptions takes a relatively long time to execute versus normal processing. Some say throwing exceptions is as much as two orders of magnitude slower than passing arguments. However, another article claims there’s almost no impact to throwing exceptions, unless you are running your software in a debugger. But most programmers recognize that "exceptions are for exceptions."

      Timm — June 4, 2009 11:15 AM
    24. Dmitri avatar

      Out of morbid curiosity, did the person who wrote this get fired?

      Dmitri — June 9, 2009 1:20 PM
    25. Josh (coding for membership blueprints) avatar

      I'd hate for any new programmer to study that for too long. Imagine the legacy that could create. I'm no programming hotshot but that could be expected of a misdirected 12 year old. A lead programmer? Umm...

      Josh (coding for membership blueprints) — August 4, 2009 11:23 PM
    26. Visual C# Kicks avatar

      This is the trippiest thing I've ever seen! Defies all logic. I really hope no one writes code like this seriously haha

      Visual C# Kicks — February 25, 2010 2:53 PM

    » Leave a Comment