Question

I have a Java class which handles both single valued and multi-valued data. Therefore, it exposes two methods: getValue() and getValues(). However, I want to throw some kind of exception when the getValue method is called for multi-valued data. What is the most relevant exception class that I can use? I feel IllegalAccessException is not appropriate, because I am not using reflection, or any such discovery technique; neither is IllegalArgumentException appropriate because I am not passing any argument as such.

Was it helpful?

Solution

UnsupportedOperationException - most common and versatile unchecked run-time exception in Java.

http://docs.oracle.com/javase/6/docs/api/java/lang/UnsupportedOperationException.html

OTHER TIPS

java.lang.IllegalStateException is the best standard exception for this.

The JavaDoc says:

Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.

In your case that means that the invoked method was called at inappropriate times.

You could make your own exception to throw specifically for this class try looking at this link

you could do something like this:

class getValException extends Exception
{
    public getValException(String message) 
    {
        super(message);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top