In the Socket class has many methods used in the statement throws throws some exceptions that are subclasses of IOException. In the method of the Socket class is thrown up SocketException, there are seven exceptions for the rest of the Socket class methods throw. The inheritance of these anomalies shown in Figure 1. Gray background box which exception is described in the methods of the Socket class may throw exceptions.
Figure 1 exception class inheritance diagram
- public class IOException extends Exception
The exception is that all the methods of the Socket class in the parent class throws exception. Therefore, when using the Socket class as long as the catch (catch) the exception for; course, in order to capture both the methods of other classes in abnormal, can also catch Exception.
- public class SocketException extends IOException
This unusual method of the Socket class to use most often. It also represents all the exceptions and networks. But if it is to know what kind of specific exception occurs, you need to capture more specific abnormal.
- public class ConnectException extends SocketException
ConnectException exception usually occurs because the server is busy or the server without the appropriate response to the listening port is not open. Such as the following statement will throw an exception ConnectException.
Socket socket = new Socket ("www.ptpress.com.cn", 1234);
- public class BindException extends SocketException
This anomaly in a number of Socket or ServerSocket object bound to the same port, but is not open when the SO_REUSEADDR option. As in the following four statements will throw an exception BindException:
Socket socket1 = new Socket ();
Socket socket2 = new Socket ();
socket1.bind (new InetSocketAddress ("127.0.0.1", 1234));
socket2.bind (new InetSocketAddress ("127.0.0.1", 1234));
- public class NoRouteToHostException extends SocketException
The exception in the event of a firewall or router can not find the host of circumstances.
- public class UnknownHostException extends IOException
This exception does not correct the domain name was thrown out. Such as the following statement will throw an exception UnKnownHostException:
Socket socket1 = new Socket ("www.ptpress123.com.cn", 80);
- public class ProtocolException extends IOException
This exception does not often throw. Due to unknown reasons, TCP / IP packet is destroyed, then an exception will be thrown ProtocolException.
- public class SocketTimeoutException extends InterruptedIOException
If the connection timeout and read timeout after the server is still not responding, connect or read methods will throw SocketTimeoutException exception.