何时应使用 NoSuchFieldException 与 NoSuchElementException?
除了 NoSuchFieldException
是受检查异常(而 NoSuchElementException
不是)之外,这两个异常之间还有什么区别?什么时候应该使用其中一种而不是另一种?
具体来说,我正在研究 OOP 表示来封装 Valve 服务器查询 A2S_INFO 数据包,其中某些值可能存在或不存在,具体取决于额外数据标志 (EDF) 的值(更具体地说,可以使用按位 AND 来测试这些值的存在具有给定的常量。) getter 方法应该提供该值(如果存在),如果不存在则抛出异常。这就是最初的问题的来源,因为我不知道在这里使用什么更有利。
下面的代码显示了字段的 getter 方法之一,该字段可能存在或不存在,具体取决于 EDF 的值:
public long getSteamID(){
if(containsSteamID){
return steamID;
}else{
//Should NoSuchElementException be used here or NoSuchFieldException?
throw new NoSuchElementException("There is no steamID in this packet.");
}
}
Aside from NoSuchFieldException
being a checked exception, where NoSuchElementException
is not, what are the differences between the two exceptions? When should one be used as opposed to the other?
Specifically, I am working on an OOP representation to encapsulate Valve Server Query A2S_INFO Packets, where some values can be present or not depending on the value for the Extra Data Flag (EDF)
(more specifically, the presence of these values can be tested using bit-wise AND with given constants.) The getter methods should provide the value if it is present, and throw an exception if not. This is where the initial question comes from, as I don't know what would be more beneficial to use here.
The code below shows one of the getter methods for a field that can be present or not depending on the value of the EDF:
public long getSteamID(){
if(containsSteamID){
return steamID;
}else{
//Should NoSuchElementException be used here or NoSuchFieldException?
throw new NoSuchElementException("There is no steamID in this packet.");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NoSuchFieldException< /code>
用于基于反射的访问,它(引用文档)“表明 [Java] 类没有指定名称的字段。”该文档还显示(至少对于已检查的异常)从哪个 JDK 方法抛出该异常。由于您没有使用 Java 反射,因此
NoSuchFieldException
似乎不合适。NoSuchElementException< /code>
可能合适,但它(至少对于 JDK)主要用于通用集合和容器类。
也许
IllegalStateException
更适合您的情况:但是,如果您正在设计一个包装器库,那么定义您自己的异常类(可能作为异常层次结构的一部分)可能是最好的解决方案。这使您可以自由地决定是否应检查异常并为其指定一个有意义的名称。这样,您的库的用户可以直接看到异常来自您的库,并且可能能够比通用
NoSuchElementException
更好地处理异常,后者也可能由完全不相关的代码引发。NoSuchFieldException
is for reflection based access, it (quoting the docs) "signals that the [Java] class doesn't have a field of a specified name." The documentation also shows (at least for checked exceptions) from which JDK methods it is thrown. Since you are not using Java reflection,NoSuchFieldException
seems inappropriate.NoSuchElementException
may be suitable, but it is (at least by the JDK) mostly used for generic collection and container classes.Maybe
IllegalStateException
would be more appropriate in your situation:However, if you are designing a wrapper library, maybe defining your own exception class (possibly as part of an exception hierarchy) would be the best solution. This gives you all the freedom to decided whether the exception should be checked or not and give it a meaningful name. This way users of your library can directly see that the exception comes from your library and possibly be able to better handle the exception than a generic
NoSuchElementException
which could have been thrown by completely unrelated code as well.