我有以下方法IndexOutOfBoundsException异常返回什么?
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
}
}
而且我不知道怎么在这里做,我敢肯定,这就是它的一个简单的办法,但我应该在catch块返回? Eclipse正在抱怨必须返回User
。
只需返回空值即可。 – Doorknob
或者没有发现异常 – Bohemian
让异常通过; getUser方法的调用者有问题。 –