IOException in Java

IOException:

java.io.IOException

Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.

Approach

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class IOExceptionExample {
    public static void main(String[] args) throws IOException {

        File file = new File("D:\\hello.txt");
        FileInputStream fileInputStream =
new FileInputStream(file);

        fileInputStream.close();
        try {
            System.out.println(fileInputStream.available());
        } catch (IOException e) {
            System.out.println("IOException occurs");

        }

    }
}

Output:

IOException occurs


Constructor of IOException

Example 1: 

java.io.IOException.IOException()

Constructs an IOException with null as its error detail message.

Java

import java.io.IOException;

public class IOExceptionExample {
    public static void main(String[] args) {
        IOException ioException = new IOException();

        System.out.println(ioException);
    }
}

Output:

java.io.IOException



Example 2:

java.io.IOException.IOException(String message)

Constructs an IOException with the specified detail message.

Parameters:

1. message The detailed message (which is saved for later retrieval by the getMessage() method)

Java

import java.io.IOException;

public class IOExceptionExample {
    public static void main(String[] args) {
        IOException ioException = new IOException("message");

        System.out.println(ioException);
    }
}

Output:

java.io.IOException: message


Example 3:

java.io.IOException.IOException(Throwable cause)

Constructs an IOException with the specified cause and detail message of (cause==null ? null: cause.toString())(which typically contains the class and detail message of cause). This constructor is useful for IO exceptions that are little more than wrappers for other throwables.

Parameters:

1. cause The cause (which is saved for later retrieval by the getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.).

Java

import java.io.FileNotFoundException;
import java.io.IOException;

public class IOExceptionExample {
    public static void main(String[] args) {
        IOException ioException =
new IOException(new FileNotFoundException());

        System.out.println(ioException);
    }
}

Output:

java.io.IOException: java.io.FileNotFoundException


Example 4:

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

Constructs an IOException with the specified detail message and cause.

Note that the detailed message associated with the cause is not automatically incorporated into this exception's detail message.

Parameters:

1. message The detailed message (which is saved for later retrieval by the getMessage() method).

2. cause The cause (which is saved for later retrieval by the getCause() method). (A null value is permitted, and indicates that the cause is non-existent or unknown.)

Java

import java.io.FileNotFoundException;
import java.io.IOException;

public class IOExceptionExample {
    public static void main(String[] args) {
        IOException ioException =
new IOException("message",new FileNotFoundException());

        System.out.println(ioException);
    }
}

Output:

java.io.IOException: message


Some other Exceptions of java.io

EOFExceptionSignals that an end of file or end of the stream has been reached unexpectedly during input.

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

InterruptedIOExceptionSignals that an I/O operation has been interrupted. 

InvalidClassExceptionThrown when the Serialization runtime detects.

InvalidObjectExceptionIndicates that one or more deserialized objects failed validation tests.

IOErrorThrown when a serious I/O error has occurred.

NotActiveExceptionThrown when serialization or deserialization is not active.

NotSerializableExceptionThrown when an instance is required to have a Serializable interface.

ObjectStreamExceptionSuperclass of all exceptions specific to Object Stream classes.

OptionalDataExceptionException 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.

StreamCorruptedExceptionThrown when control information that was read from an object stream violates internal consistency checks.

SyncFailedExceptionSignals that a sync operation has failed.

UncheckedIOExceptionWraps an IOException with an unchecked exception.

UnsupportedEncodingExceptionCharacter Encoding is not supported.

UTFDataFormatExceptionSignals 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.

WriteAbortedExceptionSignals that one of the ObjectStreamExceptions was thrown during a write operation.

No comments:

Post a Comment