NotActiveException in Java

NotActiveException:

java.io.NotActiveException

Thrown when serialization or deserialization is not active.

Example

Java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamdefaultReadObject {
    public static void main(String[] args)
throws IOException, ClassNotFoundException {

        int data = 5;
        String data2 = "Hello Java Program";
        FileOutputStream file = new FileOutputStream("D:\\hello.txt");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(file);

        objectOutputStream.writeInt(data);
        objectOutputStream.writeObject(data2);

        FileInputStream fileStream = new FileInputStream("D:\\hello.txt");
        ObjectInputStream objStream = new ObjectInputStream(fileStream);

        objectOutputStream.defaultWriteObject();
        System.out.println("Successfully write default object");
        objectOutputStream.close();
        objStream.close();

    }
}

Output:

Exception in thread "main" java.io.NotActiveException: not in call to writeObject at java.base/java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:441)


Constructor of NotActiveException

Example 1:

java.io.NotActiveException.NotActiveException()

Constructor to create a new NotActiveException without a reason.

Java

import java.io.NotActiveException;

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

        NotActiveException notActiveException =
new NotActiveException();

        System.out.println(notActiveException);
    }
}

Output:

java.io.NotActiveException


Example 2:

java.io.NotActiveException.NotActiveException(String reason)

Constructor to create a new NotActiveException with the reason given.

Parameters:

1. reason a String describing the reason for the exception.

Java

import java.io.NotActiveException;

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

        NotActiveException notActiveException =
new NotActiveException("reason");

        System.out.println(notActiveException);
    }
}

Output:

java.io.NotActiveException: reason


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.

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