Monday 28 May 2012

Exception Help

Keep find myself searching which exceptions to throw when, in regards to 'this code should never be reached' exceptions. Found a great post by Kevin Bourrillion, a software engineer at Google, which can be found here. Which I've copied below.
"
I've noticed a lot of confusion about what type of unchecked exception is the right one to throw under various circumstances. Here's a very simple explanation of the most common types.

NullPointerException
Multiple schools of thought on this one. Of course, it's thrown automatically by the runtime when you try to dereference null. Many say that you should never rely on this behavior, and should always check for null explicitly. Many also believe that when you find a null reference, you should throw IllegalArgumentException instead of NPE. This way, a thrown NPE always indicates some programming error in the implementation of the method, not a failure of the caller to pass valid parameters. I'm not taking a stand on this issue right now.

IllegalArgumentException
Throwing this exception implies that there exists at least one other value for this parameter that would have caused the check in question to pass. If the caller can't remedy this exception by substituting another value for the argument in question, it's the wrong exception to throw. Note that in some of these cases IndexOutOfBoundsException is more appropriate (and strangely, IOOBE doesn't extend IAE).

IllegalStateException
This exception implies that there are no argument values that could have caused the check to succeed, yet, there does exist at least one alternate state that the instance in question could have been in, which would have passed the check. Note that this type almost never makes sense for a static method, unless you rely heavily on static state (shame on you). Note also that this exception is appropriate whether or not it is possible to actually mutate this aspect of the instance's state, or it's already too late.

UnsupportedOperationException
This means that the method invoked will always fail for an instance of this class (concrete type), regardless of how the instance was constructed.

AssertionError
This is the right exception to use whenever a statement should by rights be impossible to reach.
"

Cheers,
Michael

5 comments:

  1. Way cоοl! Ѕome extremely νalid
    poіnts! I appreciаte you wгіting thiѕ pοst аnd the rest of the ѕitе is аlso
    ѵerу good.

    Look at my page - pikavippi
    Stop by my web blog ... pikavippi

    ReplyDelete
  2. First off I would like to say awesome blog!
    I had a quick question which I'd like to ask if you don't mind.
    I was interested to find out how you center yourself
    and clear your head before writing. I've had a hard time clearing my thoughts in getting my ideas out there. I do enjoy writing but it just seems like the first 10 to 15 minutes are usually lost just trying to figure out how to begin. Any recommendations or tips? Cheers!

    My web-site: ava fx

    ReplyDelete
  3. Hello, I check your blog daily. Your story-telling style is witty,
    keep it up!

    my web-site :: translate 350 words from english to portuguese or portuguese to english

    ReplyDelete
  4. I delight in, result in I found exactly what I used to be taking a look for.
    You've ended my 4 day long hunt! God Bless you man. Have a great day. Bye

    my homepage; http://widyawisatabanten.com

    ReplyDelete
  5. When someone writes an article he/she keeps the idea of
    a user in his/her brain that how a user can understand it.
    Thus that's why this article is great. Thanks!

    Feel free to surf to my site ... http://shekinahhouse.org

    ReplyDelete