I have the following method
private ArrayList<User> allUsers = new ArrayList<User>();
public User getUser(int index) {
try {
return allUsers.get(index);
}
catch(IndexOutOfBoundsException e) {
// What should I return here?? Say that you want index 0 and no User
// exists in the ArrayList allUsers, what should I then return? The
// method needs a User to be returned
}
}
And I’m not sure how to do here, I’m sure thats its an easy fix, but what should I return in the catch block? Eclipse is complaining that a User must be returned.
I would throw an exception if no user is found. This exception is caught when the method is called. You could modify the code here to use a custom exception if you want. Something like: