NotSerializableException in Java with Examples


NotSerializableException in Java

In Java programming, the NotSerializableException is a common exception that occurs when an object of a class is not Serializable. When an object is not Serializable, it means that the object cannot be converted into a sequence of bytes, which is required for data persistence and communication between software components.

The NotSerializableException can be thrown either by the serialization runtime or by the object instance itself. This exception is a subclass of ObjectStreamException, which is the superclass for all exceptions related to Object Stream classes. ObjectStreamException extends IOException, indicating that an I/O exception has occurred.

Since serialization is a crucial aspect of data persistence and communication in Java programming, understanding how to handle NotSerializableException is essential for developers. This exception has been a part of the Java programming language since the 1.1 version of the JDK. Therefore, it is vital for developers working with Java to be familiar with this exception and know how to handle it efficiently.

Serialization and Deserialization in Java

In Java programming, serialization is a crucial mechanism that allows objects to be converted into a byte-stream. This is particularly useful for data persistence and communication between different software components since it simplifies the transfer of objects between different parts of a system.

Various technologies such as Hibernate, RMI, JPA, EJB, and JMS commonly employ this mechanism.

Conversely, deserialization is the process of transforming a byte-stream back into an object, which is the opposite of serialization.

Platform independence, which enables objects to be serialised on one platform and deserialized on another, is a key benefit of Java's serialisation and deserialization procedure.

However, if a class instance does not implement the Serializable interface, the serialization runtime or the class instance will throw a NotSerializableException, which is named after the class.

The NotSerializableException belongs to the ObjectStreamException class, which is the superclass for all exceptions related to Object Stream classes. Furthermore, the ObjectStreamException class extends the IOException class, indicating that an I/O exception has occurred.

Consequently, Java developers who work with serialization must understand the NotSerializableException and how to handle it effectively.

NotSerializableException in Java

The controlled exceptions category includes the NotSerializableException type of exception, so methods that declare it is required to manage it. When trying to serialize an object that symbolizes a class without implementing the Serializable interface, this exception is thrown.

The complete path to import the class is java.io, and the NotSerializableException is found in the java.io package.NotSerializableException.

The ObjectStreamException class, which acts as the superclass for all exceptions connected to Object Stream classes, includes a subclass called NotSerializableException.

Additionally, the ObjectStreamException class extends the IOException class, indicating that an I/O exception has occurred. This class hierarchy ensures that exceptions related to object serialization and deserialization can be caught and handled appropriately in Java programs.

Syntax

public class NotSerializableException 
extends ObjectStreamException

NotSerializableException Structure

The NotSerializableException class has two constructors available. The first constructor, NotSerializableException(), generates a new instance of the NotSerializableException class.

Meanwhile, the second constructor, NotSerializableException(String s), creates a new instance of the NotSerializableException class but with a customized message attached to it. This message is usually the name of the class that triggered the error.

NotSerializableException Example

Algorithm

With the intention of serializing a Pair class instance, we have defined the main method of our program in this file. However, the Pair class doesn't support the Serializable interface, so a NotSerializableException is thrown.

Example

To have a better understanding, let us look at an example.

public class NotSerializableExceptionExample {
   public static void main(String[] args) {
      Pair pair = new Pair("Key1", 1);
      System.out.println("Attempting to serialize the following object: " + pair);
      Serializer.serialize(pair); // This program throws a NotSerializableException
   }
}

Output

NotSerializableExceptionExample.java:3: error: cannot find symbol
        Pair pair = new Pair("Key1", 1);
        ^
  symbol:   class Pair
  location: class NotSerializableExceptionExample
NotSerializableExceptionExample.java:3: error: cannot find symbol
        Pair pair = new Pair("Key1", 1);
^
  symbol:   class Pair
  location: class NotSerializableExceptionExample
NotSerializableExceptionExample.java:6: error: cannot find symbol
        Serializer.serialize(pair); // This program throws a NotSerializableException
^
  symbol:   variable Serializer
  location: class NotSerializableExceptionExample

How to Deal With NotSerializableException

If you encounter a NotSerializableException, the easiest way to resolve it is by making the class that throws the exception implement the Serializable interface. Nevertheless, this may not be a practical solution in situations where the class is part of a third-party library.

Another approach involves examining the class for references to non-serializable objects that do not require serialization. In this case, you can declare such objects as "transient," which means they will be ignored by the serializable runtime during serialization.

Conclusion

The NotSerializableException is a crucial exception that Java developers should be aware of when working with serialization and deserialization. The exception can occur when an object of a class is not Serializable, and handling it efficiently is essential for data persistence and communication between software components. To resolve the exception, developers can either implement the Serializable interface in the class or declare non-serializable objects as "transient."

Updated on: 15-May-2023

598 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements