OptionalDataException in Java

java.io.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.

This exception may be thrown in two cases:

1. An attempt was made to read an object when the next element in the stream is primitive data. In this case, the OptionalDataException'slength field is set to the number of bytes of primitive data immediately readable from the stream, and the eof field is set to false.

2. An attempt was made to read past the end of data consumable by a class-defined readObject or readExternal method. In this case, the theOptionalDataException's eof field is set to true, and the length field is set to 0.


Example

Java

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OptionalDataException;

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

        InputStream in = new FileInputStream("D:\\hello.txt");
        ObjectInputStream objectInputStream = new ObjectInputStream(in);

        try {
            System.out.println(objectInputStream.readObject());
        } catch (ClassNotFoundException e) {
            System.out.println("ClassNotFoundException occurs");
        } catch (OptionalDataException e) {
            System.out.println("OptionalDataException occurs");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }
        objectInputStream.close();

    }
}

Output:

OptionalDataException occurs


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.

IOExceptionSignals that an I/O exception of some sort 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.

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