Consider a function of the following general form:
Foo findFoo(Collection foos, otherarguments)
throws ObjectNotFoundException {
for(Foo foo :
With Java 8, I would recommend using an Optional for this use case.
Optional findUserByName(Collection users, String name){
Optional value = users
.stream()
.filter(a -> a.equals(name))
.findFirst();
}
This also makes it very clear to the caller that the optional can be empty if the value is not found. If you really want to throw an exception, you can use orElseThrows in Optional to achieve it.