gpt4 book ai didi

java - java中处理多个异常

转载 作者:行者123 更新时间:2023-12-01 13:06:50 25 4
gpt4 key购买 nike

我的abandon()可能会抛出AbandonException

在处理异常时,如果 Vector 中剩余一些元素,我必须调用相同的方法。

我应该如何进行?如果我没有思考清楚,最好的解决方案是什么?

   if (i + 1 < lc.size()) {
try {
lc.get(i + 1).abondon();
}
catch (AbandonException e1) {
lc.get(i+2).abandon();}}

最佳答案

以下是一些伪代码:

List errorIndexList = new ArrayList();

for(...) {
if (i + 1 < lc.size()) {
try {
lc.get(i + 1).abondon();
} catch (AbandonException e1) {
errorIndexList.add(i+1);
// do some error handle work ..
// print error log/info if need,
continue; // this is optional, in case it's the last statement,
}
}
}

// use errorIndexList to handle your errors, if need,

关于java - java中处理多个异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23200998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com