UncheckedIOException in Java

UncheckedIOException:

java.io.UncheckedIOException

Wraps an IOException with an unchecked exception.

Constructor  of UncheckedIOException

Example 1:

java.io.UncheckedIOException.UncheckedIOException(IOException cause)

Constructs an instance of this class.

Parameters:

1. cause the IOException.

Throws:

NullPointerException - if the cause is null

Approach 1: When the cause is not null

Java

import java.io.IOException;
import java.io.UncheckedIOException;

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

        UncheckedIOException uncheckedIOException =
new UncheckedIOException(new IOException());

        System.out.println(uncheckedIOException);
    }
}

Output:

java.io.UncheckedIOException: java.io.IOException


Approach 2: When the cause is null

Java

import java.io.UncheckedIOException;

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

        UncheckedIOException uncheckedIOException = new UncheckedIOException(null);

        System.out.println(uncheckedIOException);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.io.UncheckedIOException.<init>(UncheckedIOException.java:63)


Example 2:

java.io.UncheckedIOException.UncheckedIOException(String message, IOException cause)

Constructs an instance of this class.

Parameters:

1. message the detailed message, which can be null.

2. cause the IOException.

Throws:

NullPointerException - if the cause is null

Approach 1: When the cause is not null

Java

import java.io.IOException;
import java.io.UncheckedIOException;

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

        UncheckedIOException uncheckedIOException =
new UncheckedIOException("message", new IOException());

        System.out.println(uncheckedIOException);
    }
}

Output:

java.io.UncheckedIOException: message


Approach 2: When the cause is null

Java

import java.io.UncheckedIOException;

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

        UncheckedIOException uncheckedIOException =
new UncheckedIOException("message", null);

        System.out.println(uncheckedIOException);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.io.UncheckedIOException.<init>(UncheckedIOException.java:50)


Some other Exceptions of java.io

EOFException: Signals that an end of file or end of the stream has been reached unexpectedly during input.

FileNotFoundException: Signals that an attempt to open the file denoted by a specified path name has failed.

InterruptedIOException: Signals that an I/O operation has been interrupted. 

InvalidClassException: Thrown when the Serialization runtime detects.

InvalidObjectException: Indicates that one or more deserialized objects failed validation tests.

IOError: Thrown when a serious I/O error has occurred.

IOException: Signals that an I/O exception of some sort has occurred.

NotActiveException: Thrown when serialization or deserialization is not active.

NotSerializableException: Thrown when an instance is required to have a Serializable interface.

ObjectStreamException: Superclass of all exceptions specific to Object Stream classes.

OptionalDataException: Exception indicating the failure of an object read operation due to unread primitive data, or the end of data belonging to a serialized object in the stream.

StreamCorruptedException: Thrown when control information that was read from an object stream violates internal consistency checks.

SyncFailedException: Signals that a sync operation has failed.

UnsupportedEncodingException: Character Encoding is not supported.

UTFDataFormatException: Signals that a malformed string in modified UTF-8 format has been read in a data input stream or by any class that implements the data input interface.

WriteAbortedException: Signals that one of the ObjectStreamExceptions was thrown during a write operation.

No comments:

Post a Comment