SocketException class in Java

java.net.SocketException

Thrown to indicate that there is an error creating or accessing a Socket.

Declaration

public class SocketException extends IOException {
    @java.io.Serial
    private static final long serialVersionUID = -5935874303556886934L;

 
    public SocketException(String msg) {
        super(msg);
    }

    public SocketException() {
    }
}


Methods

1. SocketException()

java.net.SocketException.SocketException()

Constructs a new SocketException with no detail message.

Approach

Java

package com.SocketException;

import java.net.SocketException;

public class SocketException1 {
    public static void main(String[] args) {

        SocketException socketException = new SocketException();

        System.out.println(socketException);
    }
}

Output:

java.net.SocketException


2. SocketException(String msg)

java.net.SocketException.SocketException(String msg)

This method takes one argument. This method constructs a new SocketException with the specified detail message.

Parameters: One parameter is required for this method.

msg: the detail message.

Approach

Java

package com.SocketException;

import java.net.SocketException;

public class SocketException2 {
    public static void main(String[] args) {

        String msg = "error occured";
        SocketException socketException = new SocketException(msg);

        System.out.println(socketException);
    }
}

Output:

java.net.SocketException: error occured

No comments:

Post a Comment