Handling Network Errors
try { // connect to network host // perform network IO } catch (UnknownHostException ex) { System.err.println("Unknown host."); } catch (NoRouteToHostException ex) { System.err.println("Unreachable host."); } catch (ConnectException ex) { System.err.println("Connect refused."); } catch (IOException ex) { System.err.println(ex.getMessage()); } |
In this phrase, we illustrate the series of exceptions you should attempt to catch when performing network operations.
The first exception we try to catch is the UnknownHostException. This is a subclass of IOException and is thrown to indicate that the IP address of a host could not be determined.
NoRouteToHostException and the ConnectException are subclasses of SocketException. ...
Get Java™ Phrasebook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.