7

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.

Neel
  • 2,100
  • 5
  • 24
  • 47
  • 2
    Sorry, but something smells funny to me. Could you tell us more about your program structure and even perhaps show some code? Maybe you only need one method, and have it take a parameter that tells the class how many values to return. I don't know based on what you've posted above. And regarding your question, sometimes it is easiest and best to create your own Exception class for this. But again, let's first see if you actually need this. – Hovercraft Full Of Eels Apr 01 '12 at 16:18
  • The code is somewhat like this: class{ private Collection values; // returns single value, or throws exception public T getValue(); // returns multiple values or single value wrapped in a collection public Collection getValues(); } – Neel Apr 01 '12 at 16:31

3 Answers3

21

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

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

9

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.

A.H.
  • 63,967
  • 15
  • 92
  • 126
  • 1
    Sounds a bit unsuitable for my case, because my object is not in an *illegal* state per se. – Neel Apr 01 '12 at 16:30
  • Ummm, may not be an inappropriate time, right? But the fact is since my object is supposed to be multi-valued, which value should be returned by getValue - this is indeterminate (the zero-th value may not be expected) – Neel Apr 01 '12 at 16:38
  • IllegalStateException is commonly used for classes with a state flag/indicator; e.g. input/output class with boolean toggle for switching from one to the other. since the class is not per se a state-based class (state value is derived from the data), this is not a best case to use ISEx; though the final decision should be based on the exact implementation. ISEx is commonly used for IO classes, iterators etc, if OP's class works in this way ISEx is a good choice too. –  Apr 01 '12 at 16:44
0

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);
    }
}
chefburns
  • 407
  • 3
  • 12
  • 2
    Maybe creating my own exception may not be a great idea unless absolutely necessary because that would require people to learn a new API, correct? – Neel Apr 01 '12 at 16:31
  • 2
    Bloch says in Effective Java 2nd that creating new exception when there are already existing ones that are appropriate is a bad coding practice. –  Apr 01 '12 at 16:42