Java – throw an exception on a method call

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
分享
二维码
< <上一篇
下一篇>>