gpt4 book ai didi

java - 从一个 catch 移动到另一个 try catch block

转载 作者:行者123 更新时间:2023-12-01 14:20:44 25 4
gpt4 key购买 nike

我有多个 try catch block 。我想制作我的程序,以便如果在第一个 block 中找不到某些内容,则该 block 的捕获将继续它到另一组 try catch。我做了一些研究,但未能提出解决方案。

try{
Something
} catch (Exception e) {
Move to next set of try catch
}

这就是我想要实现的。

最佳答案

来自 comment :

I have 4 Elements for which I need to create try catch blocks, i.e. 4 try catch blocks

要尝试 4 个不同的代码块,您可以这样写:

try {
// Attempt 1
} catch (Exception e1) {
try {
// Attempt 2
} catch (Exception e2) {
try {
// Attempt 3
} catch (Exception e3) {
try {
// Attempt 4
} catch (Exception e4) {
RuntimeException e = new RuntimeException("All attempts failed", e1);
e.addSuppressed(e2);
e.addSuppressed(e3);
e.addSuppressed(e4);
throw e;
}
}
}
}

关于java - 从一个 catch 移动到另一个 try catch block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62587952/

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