java - 抛出异常但不停止程序的最佳方法是什么?
问题描述
请在下面找到我需要的示例。确实,我不想关闭整个过程,因为即使它抛出异常,它也可以继续。
环形
// set a lot of variables and execute some methods
for (int i = 0, i < items.length; i++) {
// blablabla
myMethod()
// blablabla2
}
// some code here also
我的方法()
// blabla
if (!found)
throw new EndCurrentProcessException()
// blabla
EndCurrentProcessException
public void EndCurrentProcessException() {
??? What I'm supposed to put here to stop the loop iteration ???
}
也许使用throw new
的不是好方法。
我希望很清楚,如果不是,请随时向我询问更多信息。
解决方案
尝试使用 try-catch 语句。
for (int i = 0, i < items.length; i++) {
// blablabla
try {
myMethod();
} catch(EndCurrentProcessException e){
// do something or continue;
continue;
}
// blablabla2
}
// some code here also
推荐阅读
- bash - 如何将跨所有项目运行的 GCP Cloud Shell 命令与特定于一个项目的命令结合起来?
- python - 当我追加一个新列表时,为什么我的数组中的所有列表都会改变?
- sql - 如何在 SQL 中将查询结果作为附件发送
- javascript - 数组中的 Javascripts 方法
- jquery - 我客户网站的搜索建议功能在 Firefox 中不起作用,但在 Chrome 和 Safari 中起作用
- dart - 我正在尝试使用 TextField 的控制器,但我收到错误“NoSuchMethodError:方法'调用'被调用为空”
- python - Pandas:将函数应用于每个 float64,但获得不可散列的类型
- jquery - 带有树级别的引导折叠树视图(使用 CSS)
- laravel - 如何在 db-select 中执行自动增量?(拉拉维尔)
- javascript - waitForKeyElement.js 但有最长等待时间