Java: Serialization Exception Handling

All these exceptions are related to serialization which occurred while doing serialization and deserialization of objects. These exceptions are subclasses of ObjectStreamException which is a subclass of IOException.

Pre-requisite:

Java Serialization exception hierarchy

Exception Description
ObjectStreamException Superclass of all serialization exceptions.
InvalidClassException This exception occurred when  a class cannot be used to restore/deserialize objects for any of these reasons:

  • The class does not match the serialversionUID of the class in the stream.
  • The class contains properties with invalid primitive data types.
  • The Externalizable class doesn’t have a public no-arg constructor.
  • The  Serializable class can’t access the no-arg constructor of its closest non-serializable superclass.
NotSerializableException This exception has thrown by a readObject or writeObject method to terminate serialization or deserialization.
StreamCorruptedException This exception has thrown when some of the meta information modified:

  • If the stream header is invalid.
  • If control information not found.
  • If control information is invalid.
  • JDK 1.1.5 or fewer attempts to call readExternal on a PROTOCOL_VERSION_2 stream.
NotActiveException This exception has thrown if writeObject state is invalid within the following ObjectOutputStream methods:

  • defaultWriteObject
  • putFields
  • writeFields

This exception has thrown if readObject state is invalid within the following ObjectInputStream methods:

  • defaultReadObject
  • readFields
  • registerValidation
InvalidObjectException This exception occured when a restored object cannot be made valid.
OptionalDataException This exception has thrown by readObject when there is primitive data in the stream and an object is expected. The length field of the exception represents the number of bytes that are available in the current block.
WriteAbortedException This exception has thrown when reading a stream terminated by an exception that occurred while the stream was being written.

See Also:

References