Java network programming from getting started to mastering (19): Socket (SOCKET) exceptions

Source: Internet
Author: User

There are a number of methods in the socket class that use throws to throw exceptions when declared, all of which are subclasses of IOException. SocketException is the most common method in the socket class, and there are seven exceptions that can be thrown by the method of the socket class. These exceptions are shown in inheritance relationship 1. The exception that is described in the gray background box is the exception that may be thrown by the method of the socket class.


Figure 1 Exception class inheritance diagram

    • public class IOException extends Exception

This exception is the parent class for all exceptions thrown in the method of the socket class. Therefore, when using the socket class, just catch (catch) This exception can be, of course, in order to catch the exception of the methods in other classes, you can also directly capture exception.

    • public class SocketException extends IOException

This exception is most frequently used in the methods of the socket class. It also represents all network-related exceptions. However, if you want to know what kind of anomalies occur, you need to catch more specific exceptions.

    • public class Connectexception extends SocketException

Connectexception exceptions typically occur because the server is busy and unresponsive, or the server's corresponding listening port is not open. A Connectexception exception is thrown as the following statement.

Socket socket = new Socket ("www.ptpress.com.cn", 1234);
    • public class Bindexception extends SocketException

This exception occurs when multiple sockets or ServerSocket objects are bound to the same port, and the SO_REUSEADDR option is not turned on. The following four statements will throw a Bindexception exception:

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

This exception occurs when the host is not found by a firewall or by routing.

    • public class Unknownhostexception extends IOException

This exception is thrown when the domain name is incorrect. The following statement throws a Unknownhostexception exception:

Socket SOCKET1 = new socket ("www.ptpress123.com.cn", 80);
    • public class Protocolexception extends IOException

This exception is not often thrown. For unknown reasons, TCP/IP packets are corrupted, and a Protocolexception exception is thrown.

    • public class Sockettimeoutexception extends Interruptedioexception

If the server still does not respond after the connection time-out and the read data time-out period, the Connect or Read method throws a Sockettimeoutexception exception.

Java network programming from getting started to mastering (19): Socket (SOCKET) exceptions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.