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. NoRouteToHostException signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the remote host cannot be reached because of a problem with an intervening firewall or router. ConnectException is thrown if a connection is refused to the remote host. IOException is a more general purpose exception that can also be thrown from networking calls.

The phrases throughout this chapter and Chapter 11, "Network Servers," do not include error handling. Catching these exceptions is what you would do in your Java applications that use networking functionality.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net