抛出异常后如何继续执行Java程序?
我的示例代码如下:
public class ExceptionsDemo { public static void main(String[] args) {
try {
int arr[]={1,2,3,4,5,6,7,8,9,10};
for(int i=arr.length;i<10;i++){
if(i%2==0){
System.out.println("i =" + i);
throw new Exception();
}
}
} catch (Exception e) {
System.err.println("An exception was thrown");
}
}
}
我的要求是,在捕获到异常之后,我要处理数组的其余元素。我怎样才能做到这一点?
回答:
您的代码应如下所示:
public class ExceptionsDemo { public static void main(String[] args) {
for (int i=args.length;i<10;i++){
try {
if(i%2==0){
System.out.println("i =" + i);
throw new Exception(); // stuff that might throw
}
} catch (Exception e) {
System.err.println("An exception was thrown");
}
}
}
}
以上是 抛出异常后如何继续执行Java程序? 的全部内容, 来源链接: utcz.com/qa/426215.html