2013-01-10

Java programming: Utilize UnsupportedOperationException

One of the big differences between Eclipse and Netbeans is the way they both populate their default pre-configured code templates.
Software developers working with Netbeans will probably appreciate the fact that all your implementation methods will automatically look like that:

@Override
public Object methodWithObjectReturnValue() {
    throw new UnsupportedOperationException();
}

@Override
public Object methodWithIntReturnValue() {
    throw new UnsupportedOperationException();
}

Eclipse on the other hand will create a methods that will like like that:

@Override
public Object methodWithObjectReturnValue() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int methodWithIntReturnValue() {
    // TODO Auto-generated method stub
    return 0;
}


I'll leave it as an exercise for the reader to figure out, why I think that throwing an UnsupportedOperationException is by far the better choice... :-)
So... if you're on Eclipse make sure to adjust your Java Code Templates and throw these pretty UnsupportedOperationExceptions in all the right places before you start to actually implement your stuff!

... oh... and one more piece of advice: don't you confuse Java's UnsupportedOperationException with OperationNotSupportedException. +Dustin Marx wrote a nice article about that topic some years ago that you might want to check out!

No comments:

Post a Comment