IllegalStateException的预期用途是什么? [英] What's the intended use of IllegalStateException?

查看:217
本文介绍了IllegalStateException的预期用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Java的Javaadocs IllegalStateException 表示:


表示在非法或不适当的时间调用了一种方法。换句话说,Java环境或Java应用程序不适用于所请求的操作。


有效的Java表示(项目60,第248页):


另一个常见的重用异常是IllegalStateException。如果调用由于接收对象的状态而非法,那么这通常是异常的。例如,如果调用者在正确初始化之前尝试使用某些对象,则抛出异常。


似乎这里有一些差异。 javadocs的第二个句子使得它听起来像是可以描述关于Java执行状态的非常广泛的条件,但是在Effective Java中的描述使其听起来像它用于与具体关于对象的状态的状态相关的条件方法已被调用。



我在JDK中看到的用法(例如集合, Matcher )和番石榴绝对似乎属于有效Java谈论的类别(此对象处于无法调用此方法的状态)。这也似乎与 IllegalStateException 的兄弟姐妹 IllegalArgumentException



JDK中是否存在与Java环境或Java应用程序相关的任何合法的 IllegalStateException ?还是做最佳实践指南,倡导将其用于更广泛的执行状态?如果没有,为什么这样的字眼是这样的吗? ;)

解决方案

这是JDK中这个异常的一个特别合法的用法(见: 其他300多种用法中的URLConnection.setIfModifiedSince(long)

  public void setIfModifiedSince(long ifmodifiedsince){
if(connected)
throw new IllegalStateException(Already connected);
ifModifiedSince = ifmodifiedsince;
}

我认为这个例子很清楚,如果对象特别是状态(已经连接)有些操作不应该被调用,在这种情况下,当连接建立时,一些属性不能设置。



当你的类有一些状态机器?)随着时间的推移而变化我的方法无关紧要或不可能。考虑一个 Car 类,它有 start() stop() fuel()方法。在调用 start()两次之前,一个接一个地说,可能没有错,但加油一辆开车肯定是一个坏主意。也就是说,汽车处于错误状态。



可以说好的API不应该允许我们在错误的状态下调用方法,以便在编译时发现这样的问题,而不是在运行。在这个特定示例中,连接到URL应该返回一个不同的方法子集,所有这些对象在连接后都是有效的。


This came up in a discussion with a colleague today.

The Javadocs for Java's IllegalStateException state that it:

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.

And Effective Java says (Item 60, page 248):

Another commonly reused exception is IllegalStateException. This is generally the exception to throw if the invocation is illegal because of the state of the receiving object. For example, this would be the exception to throw if the caller attempted to use some object before it had been properly initialized.

It seems there's a bit of discrepancy here. The second sentence of the javadocs makes it sound like the exception could describe a very broad condition about the Java execution state, but the description in Effective Java makes it sound like it's used for conditions related specifically to the state of the state of the object whose method has been called.

The usages I've seen in the JDK (e.g. collections, Matcher) and in Guava definitely seem to fall into the category that Effective Java talks about ("This object is in a state where this method can't be called"). This also seems consistent with IllegalStateException's sibling IllegalArgumentException.

Are there any legitimate IllegalStateException usages in the JDK that do relate to the "Java environment" or "Java application"? Or do any best practices guides advocate using it for the broader execution state? If not, why the heck are the javadocs phrased like that? ;)

解决方案

Here is one particularly legitimate usage of this exception in JDK (see: URLConnection.setIfModifiedSince(long) among 300+ other usages of it:

public void setIfModifiedSince(long ifmodifiedsince) {
    if (connected)
        throw new IllegalStateException("Already connected");
    ifModifiedSince = ifmodifiedsince;
}

I think the example is pretty clear. If the object is in particular state ("Already connected"), some operations should not be called. In this case when connection was established, some properties cannot be set.

This exception is especially useful when your class has some state (state machine?) that changes over time, making some methods irrelevant or impossible. Think about a Car class that has start(), stop() and fuel() methods. While calling start() twice, one after another, is probably nothing wrong, but fueling a started car is certainly a bad idea. Namely - car is in a wrong state.

Arguably good API should not allow us to call methods in wrong state so that problems like that are discovered at compile time, not at runtime. In this particular example connecting to a URL should return a different object with a subset of methods, all of which are valid after connecting.

这篇关于IllegalStateException的预期用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆