- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有以下方法
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 block 中返回什么? Eclipse 提示必须返回一个 User
。
最佳答案
我的一般意见是,您不应该永远不要捕获您不知道如何处理的异常。特别是在这种情况下,由于 IndexOutOfBoundsException
是一个 RuntimeException
,因此不需要被捕获 - 您也可以让它向上传播调用堆栈。调用者通过列表索引请求一个对象,因此大概知道要请求哪个索引——然后,抛出或允许抛出的 IndexOutOfBoundsException 传播似乎非常自然。
唯一其他明显的选择是吞下异常并返回 null
,但我真的不喜欢这种方法,因为在没有明智的情况下,调用方会出现这种明显的错误返回值。您还可以返回一个特殊的 User
实例(引用 null object pattern),但即使这样也不能免除调用者检查返回内容的责任。根据 User
的接口(interface)和实现,此类检查可能微不足道,但仍需要在某处完成。
如果你想明确该方法可以抛出异常,就这么说:
public User getUser(int index) throws IndexOutOfBoundsException { ... }
或点赞@ Bela Vizer建议,将其包装在 IllegalArgumentException(这也是一个 RuntimeException)中。
正如@ lc. 指出的那样,最好先自己检查对象是否存在,然后再尝试访问它。自己处理您预期 的错误情况,而不是依赖 get()
方法调用来抛出异常。您仍然应该清楚方法可能会抛出这样的异常,但是,如果集合在检查和返回之间被修改。对于多核 CPU 上的多线程软件,已知会发生奇怪的事情。
关于java - 在 IndexOutOfBoundsException 异常时返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433438/
这个问题已经有答案了: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? (25 个回答)
((fn [coll] (letfn [(add-item [acc coll idx] (conj acc (nth coll idx))
我不是 Java 初学者,但也不是专家,这就是我发布此文章以寻求帮助/解释的原因。我在互联网上查找了很多地方,但没有找到我正在寻找的答案。 public class Driver { public s
我不断得到 线程“main”中出现异常 java.lang.ArrayIndexOutOfBoundsException:MergeSorter.merge(MergeSorter.java:44)、
我做了一个小射击游戏..它工作正常但我也想实现如果火相交它们会消失。我有两个播放器子弹列表和计算机子弹列表......但是如果我有更多来自计算机或反向的子弹。这是我的循环 for (int
我试图从 JOptionPane 读取像 3+9-2*10/5 这样的数学表达式并得到它的结果——当然要考虑到操作顺序。我使用 String.split() 将字符串拆分为数字和操作数,并创建了一个寻
我在Grails中有以下代码: public LibraryItem createLibraryItemWithValues(ProjectItem projectItem) { def li
这个问题已经有答案了: What is a debugger and how can it help me diagnose problems? (3 个回答) 已关闭 7 年前。 我有一组以下类型的
请看下面的代码 DatabaseHandler.java public List getDetails(String name) { List details = new Ar
我应该让蛇形矩形沿着屏幕的一侧移动。然而它保持在原位,当我运行它时,我还收到 IndexOutOfBoundsException 错误。我相信这与我的 Render.java 文件中的 for 循环有
ArrayList beds = new ArrayList(49); public Patient getPatient(int bedNumber) { if (beds.get(bedN
我有 10 张卡片的数组。在“回合”的过程中,我向数组添加一张卡片,然后从数组中删除一张卡片(并不总是相同的)。该代码在前 6-7 回合中效果很好,然后抛出 IndexOutofBoundsExcep
我正在尝试创建一个评分系统并获得最佳分数,但我的方法生成了 IndexOutOfBoundsException,但我找不到超出数组列表范围的内容,有人可以帮助我吗? 代码: public stati
为什么 int row = 1; // multiArray[col].length = 6 while(multiArray[col][row] > 1 && row 1) { 关于java -
我制作了一款游戏,我们控制一艘宇宙飞船,可以发射激光摧毁来自顶部的小行星。但在游戏过程中,每当它抛出 IndexOutOfBoundsException 时,我的游戏就会卡住。我不知道为什么会这样。请
我正在尝试实现合并排序,但我不断收到 IndexOutOfBoundsException 并且我无法找出原因。 我已经调试了我的程序,并且在此函数中我从未使用无效索引访问数组。引发异常的行也与抛出异常
我无法弄清楚为什么我的代码出现 IndexOutOfBoundsException。我想知道是否有人可以成为我的额外眼睛来帮助我发现错误。我正在手动尝试这个,但我认为在浏览代码时我遗漏了一些东西。 代
为什么我会收到此错误? Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 at java.util.
我不知道这是否是一个简单的问题,但我只是看不出问题是什么。我现在从 Google Play 中的应用收到了三份关于 points.get(++i) 处的 IndexOutOfBoundsExcepti
如何执行以下内容 String p = "abcd"; System.out.print(p.substring(4)); 不会导致java.lang.IndexOutOfBoundsExceptio
我是一名优秀的程序员,十分优秀!