问题描述
我对使用异常感到困惑: IllegalStateException 与 UnsupportedOperationException .
I'm confusing of using exception: IllegalStateException vs UnsupportedOperationException.
我有一个delete方法,在某些情况下不允许使用:假设呼叫者上有有效数据.
I have a delete method which in some cases do not allow to use: let's say when the caller has a valid data on it.
然后我应该向用户提供一个异常信息,说明他现在正在执行无效操作.
Then I should give an exception info to user that he is doing an invalid operation now.
那么,我应该抛出哪个异常? IllegalStateException 或 UnsupportedOperationException .
So, which exception should I throw? IllegalStateException or UnsupportedOperationException.
我知道我可以使用其中任何一个来提供详细信息,但是我仍然想知道哪种情况更好.
I know I can give the detail message using any of them, but I still want to know which one is better in my case.
来自JavaDoc:
- IllegalStateException:
表示已在非法或不适当的时间调用了方法.换句话说,对于所请求的操作,Java环境或Java应用程序没有处于适当的状态.
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.
- UnsupportedOperationException:
抛出该错误以指示不支持请求的操作.
Thrown to indicate that the requested operation is not supported.
推荐答案
UnsupportedOperationException
应该被使用,因为 IllegalStateException 根本不被支持.应该使用code>,因为该方法受支持,但在当前状态下不合法.
UnsupportedOperationException
should be used as the method is not supported at all while IllegalStateException
should be used as the method is supported but that in the current state, it is not legal.
Iterator
类是很好的候选者,用于说明这两个异常之间的区别.
The Iterator
classes are good candidates to illustrate the difference between these two exceptions.
Iterator
通过抛出 UnsupportedOperationException
来以默认方法实现 remove()
:
Iterator
implements remove()
in a default method by throwing a UnsupportedOperationException
:
public interface Iterator<E> {
...
default void remove() {
throw new UnsupportedOperationException("remove");
}
...
}
不重写该方法以支持该方法的实现确实从未支持该方法.
The method is indeed never supported by implementations that don't override this method to support it.
关于实现,我们可以看到 ArrayList
类重写 remove()
来支持它.因此,不再抛出 UnsupportedOperationException
.
About implementations, we can see that the Iterator
implementation used by the ArrayList
class overrides remove()
to support it. So UnsupportedOperationException
is not thrown any longer.
另一方面,如果您在从未调用 next()
来使迭代器进行迭代的过程中调用该方法,则该方法将引发 IllegalStateException
.下一个元素:
On the other hand, we can also see that the method throws an IllegalStateException
if you invoke it while you never invoked next()
to make progress the iterator to the next element :
private class Itr implements Iterator<E> {
...
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
...
}
}
此实现为此方法提供了支持,但是如果您在非法状态下调用该方法,则会抛出 IllegalStateException
.
The method is so supported by this implementation but if you invoke it in a illegal state, an IllegalStateException
is thrown.
这篇关于IllegalStateException与UnsupportedOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!