gpt4 book ai didi

Java:掉落到下一个 `catch` 子句

转载 作者:行者123 更新时间:2023-11-29 07:25:36 25 4
gpt4 key购买 nike

我有以下结构:

try {
Request.Get(url).execute() // Apache Fluent
// do some other stuff
} catch (HttpResponseException e) {
if (e.getStatusCode() != 404) {
//drop to next catch clause
}
handle404(e);
} catch (IOException e) {
handleGenericIOException(e);
}

我无法弄清楚 if 语句中的内容。我只想说“如果异常不是 404,就好像这个子句从未捕获它一样”。但是简单地调用 throw e 只是将它从方法中抛出。有没有办法传递到下一个 catch 子句?

最佳答案

嵌套您的异常处理程序。

try {
try {
Request.Get(url).execute() // Apache Fluent
// do some other stuff
} catch (HttpResponseException e) {
if (e.getStatusCode() != 404) {
throw e;
}
handle404(e);
}
} catch (IOException e) {
handleGenericIOException(e);
}

重新抛出将在外部 try/catch block 中被捕获,因为 HttpResponseExceptionIOException 的子类。

关于Java:掉落到下一个 `catch` 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53707975/

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