我有以下方法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方法的調用者有問題。 –