Consider a function of the following general form:
Foo findFoo(Collection<Foo> foos, otherarguments)
throws ObjectNotFoundException {
for(Foo foo : foos){
if(/* foo meets some condition*/){
return foo;
}
}
throw new ObjectNotFoundException();
}
A concrete case, for example, would be:
User findUserByName(Collection<User> users, String name)
throws ObjectNotFoundException {
for(User user : users){
if(user.getName().equals(name)){
return user;
}
}
throw new ObjectNotFoundException();
}
These functions throw an exception if the object is not found. I can create a custom exception class for this purpose (in the examples, ObjectNotFoundException
) but I would prefer to use an existing class. However, I could not find any exception class with this meaning in the standard java library. Do you know if there is a standard exception that can be used here?