我有以下结构:
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 中被捕获,因为 HttpResponseException
是 IOException
的子类。
关于Java:掉落到下一个 `catch` 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53707975/