Catching Exceptions Caused by Temporary Network Errors

I need to download some URLs, and it may happen that some kind of exception may occur during operations, for example java.net.NoRouteToHostException. I consider this a temporary exception, as the download may be successful if you try again after a while.
Therefore, I would like to catch all those exceptions related to timing errors, and I started with them: java.net.NoRouteToHostException, java.net.SocketException
Can you list any other exceptions, such as these?

+3
source share
4 answers

java.net.SocketExceptionis a parent java.net.NoRouteToHostException, so you can only catch a parent. The remaining children are SocketExceptionequal BindException, ConnectExceptionand PortUnreachableException. Maybe it’s okay to catch only SocketExceptionin your case ...

+4
source

One pattern that is sometimes useful for solving temporary errors in repeated actions is to define the error callback interface and some classes that implement it; when an error occurs, let the callback know and let it decide whether to throw an exception or whether the action should be repeated. Since existing communication classes do not match this pattern, it may be necessary to catch their exceptions and turn them into callbacks.

, , . , , , .., , - "".

+1

@reef java.net.NoRouteToHostException exntends java.net.SocketException, java.net.SocketException, . java.net.SocketException IOException, , , . IO .

0

HttpRetryException - . , java.net, SocketException?

( )

try {
....
} catch(Exception e) {
    if(e.getClass().getPackage().getName().equalsIgnoreCase("java.net")) {
        retry();
    }
}
0

Source: https://habr.com/ru/post/1794481/


All Articles