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.");
}
}