捕获临时网络错误引起的异常
我必须下载一些URL,并且在操作过程中可能会发生一些异常,例如java.net.NoRouteToHostException
。我认为这是一个临时错误异常,因为如果您在一段时间后重试,下载可能会成功。
因此,我想捕获与临时错误相关的所有异常,我从以下开始:java.net.NoRouteToHostException
、java.net.SocketException
< br> 您能列出其他类似的例外情况吗?
I have to download some URLs, and it can happen that during the operations some exception may occur, like java.net.NoRouteToHostException
. I consider it a temporary error exception, as the download can succeed if you retry it after some time.
So, I'd like to catch all those exceptions related to temporary errors, and I've started with these: java.net.NoRouteToHostException
, java.net.SocketException
Can you list other exceptions like these?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
java.net.SocketException
是java.net.NoRouteToHostException
的父级,因此您可能只能捕获父级。SocketException
的其他子异常是BindException
、ConnectException
和PortUnreachableException
。也许在你的情况下只捕获 SocketException 就可以了......java.net.SocketException
is the parent ofjava.net.NoRouteToHostException
so you could maybe catch only the parent. The others childs ofSocketException
areBindException
,ConnectException
andPortUnreachableException
. Maybe it's ok to catch onlySocketException
in your case...有时对于处理可重试操作上的暂时性错误很有用的一种模式是定义错误回调接口和一些实现它的类;当发生错误时,让回调对象知道,并让它决定是否应该抛出异常或应该重试该操作。因为现有的通信类不遵循这样的模式,所以可能需要捕获它们的异常并将它们转换为回调。
请注意,与正常的异常处理相比,此方法可以更清晰地处理许多错误处理场景。除此之外,它可以轻松地设置可以尝试重试次数、重试可能需要多长时间等的策略,并且还提供了一种在收到“取消”请求之类的情况下跳过未来重试的方法。
One pattern which is sometimes useful for dealing with transitory errors on retry-able operations is to define an error-callback interface and some classes that implement it; when an error occurs, let the callback object know, and let it decide whether an exception should be thrown or the action should be retried. Because the existing communications classes don't follow such a pattern, it may be necessary to catch their exceptions and turn them into callbacks.
Note that this approach allows much cleaner handling of many error-handling scenarios than would normal exception handling. Among other things, it makes it easy to set policies for how many retries may be attempted, how long they may take, etc. and also provides a means of skipping future retries if something like a "cancel" request is received.
正如@reef已经说过java.net.NoRouteToHostException扩展了java.net.SocketException,所以如果你捕获java.net.SocketException就足够了。但是 java.net.SocketException 扩展了 IOException,我认为这才是您真正应该捕获的。所有 IO 问题都应该抛出此异常。
As @reef has already said java.net.NoRouteToHostException exntends java.net.SocketException, so if you catch java.net.SocketException it is enough. But java.net.SocketException extends IOException and I think that this is what you really should to catch. All IO problems should throw this exception.
HttpRetryException 是另一个。也许 java.net 中不扩展 SocketException 的所有内容?
一种快速(而且非常肮脏)的方法可能是
HttpRetryException is another. Maybe everything in java.net that does not extend SocketException?
One quick (and very dirty) way could be