Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throw an exception on a method call

How do I throw and UnsupportedOperationException on a method? So if I have an Iterable object and I'm trying to disallow the remove method for that object.

In the method below I'm returning an iterable object whose iterator's remove I need to disable by throwing an UnsupportedErrorException. Can I do this within the body of the method or how so?

  public Iterable<String> getInNodes (String destinationNodeName)  {
  if (!hasNode(destinationNodeName))
      return emptySetOfString;
  else {
  for(String e : nodeMap.get(destinationNodeName).inNodes)
  {
      emptySetOfString.add(e);
  }
  return emptySetOfString;
  }
}
like image 554
user1766888 Avatar asked May 03 '26 09:05

user1766888


1 Answers

Try this.

@Override
public void remove() {
   throw new UnsupportedOperationException();
}
like image 178
tjg184 Avatar answered May 05 '26 21:05

tjg184