java.net.BindException in Java with Examples Last Updated : 03 Mar, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The java.net.BindException is an exception that is thrown when there is an error caused in binding when an application tries to bind a socket to a local address and port. Mostly, this may occur due to 2 reasons, either the port is already in use(due to another application) or the address requested just cannot be assigned to this application. The BindException inherits from the SocketException class, thus showing there is an error related to the socket creation or access. Constructors The following constructors are available for BindException: BindException(): Creates a simple instance of the BindException class with no detailed messageBindException(String msg): Creates an instance of the BindException class with the specified message as the reason why the bind error occurred. Method Summary Methods inherited from class java.lang.Throwable: addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, setStackTrace, toString. Methods inherited from class java.lang.Object: clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait.HIERARCHY OF java.net.BindExceptionExample: In the example below, we have created a class Ex_BindException to demonstrate the BindException : Java // java.net.BindException in Java with Examples import java.io.*; import java.net.*; public class Ex_BindException { // Creating a variable PORT1 with arbitrary port value private final static int PORT1 = 8000; public static void main(String[] args) throws IOException { // Creating instance of the ServerSocket class // and binding it to the arbitrary port ServerSocket socket1 = new ServerSocket(PORT1); // Creating another instance of the ServerSocket // class and binding it to the same arbitrary // port,thus it gives a BindException. ServerSocket socket2 = new ServerSocket(PORT1); socket1.close(); socket2.close(); } } Output: In the above code, we first created an instance of the ServerSocket class using the specified port. That instance is successfully bound. However, when on the creation of another instance using the same port, a BindException occurs as the port is already bound to the other Socket. Here, we can simply use another arbitrary port (which is not in use) for the second socket to get rid of this exception. Comment More infoAdvertise with us Next Article java.net.BindException in Java with Examples A ashutoshrathi Follow Improve Article Tags : Java Technical Scripter Technical Scripter 2020 Java-Exceptions Practice Tags : Java Similar Reads java.lang.ArrayIndexOutOfBoundsExcepiton in Java with Examples The java.lang.ArrayIndexOutOfBoundsException is a runtime exception and thrown only at the execution state of the program. Java compiler never checks for this error during compilation. The java.lang.ArrayIndexOutOfBoundsException is one of the most common exceptions in java. It occurs when the progr 2 min read Collection add() Method in Java with Examples The add(E element) of java.util.Collection interface is used to add the element 'element' to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false. Syntax: Collection.add(E element) Paramet 4 min read Collection addAll() method in Java with Examples The addAll(Collection collection) of java.util.Collection interface is used to add the Collection 'collection' to this existing collection. This method returns a boolean value depicting the successfulness of the operation. If the collection was added, it returns true, else it returns false. Syntax: 3 min read java.net.SocketException in Java with Examples SocketException is a subclass of IOException so it's a checked exception. It is the most general exception that signals a problem when trying to open or access a socket. The full exception hierarchy of this error is: java.lang.Object java.lang.Throwable java.lang.Exception java.io.IOException java.n 5 min read List add(E ele) method in Java with Examples The add(E ele) method of List interface in Java is used to insert the specified element to the end of the current list. Syntax: public boolean add(E ele) Parameter: This method accepts a single parameter ele which represents the element to be inserted at the end of this list. Return Value: The funct 2 min read Like