【发布时间】:2019-01-27 04:45:03
【问题描述】:
我有一个可以工作并捕获错误的程序,但是我希望它完成循环并且在捕获错误时不停止。
public class myClass {
int[] table;
int size;
public myClass(int size) {
this.size = size;
table = new int[size];
}
public static void main(String[] args) {
int[] sizes = {5, 3, -2, 2, 6, -4};
myClass testInst;
try {
for (int i = 0; i < 6; i++) {
testInst = new myClass(sizes[i]);
System.out.println("New example size " + testInst.size);
}
}catch (NegativeArraySizeException e) {
System.out.println("Must not be a negative.");
}
}
}
由于数组大小为负数而发生错误,但是我该如何继续完成循环?
【问题讨论】:
-
将异常放入
for-loop
标签: java error-handling