9.4 Socket Exceptions
Most
methods of the Socket class are declared to throw
IOException or its subclass,
java.net.SocketException:
public class SocketException extends IOExceptionHowever, knowing that a problem occurred is often not sufficient to
deal with the problem. Did the remote host refuse the connection
because it was busy? Did the remote host refuse the connection
because no service was listening on the port? Did the connection
attempt timeout because of network congestion or because the host was
down? There are several subclasses of
SocketException that provide more information
about what went wrong and why:
public class BindException extends SocketExceptionA BindException is thrown if you try to
public class ConnectException extends SocketException
public class NoRouteToHostException extends SocketException
construct a Socket or
ServerSocket object on a local port that is in use
or that you do not have sufficient privileges to use. A
ConnectException is thrown when a
connection is refused at the remote host, which usually happens
because the host is busy or no process is listening on that port.
Finally, a
NoRouteToHostException indicates that the
connection has timed out.The java.net package also includes
ProtocolException, a direct subclass of
IOException:
public class ProtocolException extends IOExceptionThis is thrown when data is received from the network that somehow
violates the TCP/IP specification.None of these exception classes have any special methods you
wouldn't find in any other exception class, but you
can take advantage of these subclasses to provide more informative
error messages or to decide whether retrying the offending operation
is likely to be successful.
• Table of Contents• Index• Reviews• Reader Reviews• Errata• AcademicJava Network Programming, 3rd EditionBy
Elliotte Rusty Harold Publisher: O'ReillyPub Date: October 2004ISBN: 0-596-00721-3Pages: 706
Thoroughly revised to cover all the 100+ significant updates
to Java Developers Kit (JDK) 1.5, Java Network
Programming is a complete introduction to
developing network programs (both applets and applications)
using Java, covering everything from networking fundamentals
to remote method invocation (RMI). It includes chapters on
TCP and UDP sockets, multicasting protocol and content
handlers, servlets, and the new I/O API. This is the
essential resource for any serious Java developer.