Java – throw an exception on a method call
•
Java
How to throw and unsupportedoperationexception on a method? So, if I have an Iterable object and I try to disable the remove method of that object
In the following method, I return an iteratable object, and the deletion of its iterator needs to be disabled by throwing unsupported errorexception Can I do this in the method body or how?
public Iterable<String> getInNodes (String destinationNodeName) {
if (!hasNode(destinationNodeName))
return emptySetOfString;
else {
for(String e : nodeMap.get(destinationNodeName).inNodes)
{
emptySetOfString.add(e);
}
return emptySetOfString;
}
}
Solution
Try this
@Override
public void remove() {
throw new UnsupportedOperationException();
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
