Home » Java Basics » exceptions » java.io.ObjectStreamException – How to solve Object Stream Exception

About Sotirios-Efstathios Maneas

Sotirios-Efstathios Maneas
Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.

java.io.ObjectStreamException – How to solve Object Stream Exception

In this tutorial we will discuss about ObjectStreamException. This exception is defined as the superclass of all exceptions specific to Object Stream classes. The ObjectStreamException is defined as an abstract class and thus, an instance of ObjectStreamException cannot be created. Also, the ObjectStreamException extends the IOException class, which signals that an I/O exception has occurred.

There is a number of known sub-classes, such as InvalidObjectException, NotSerializableException and StreamCorruptedException. The first exception indicates that one or more deserialized objects failed the validation tests. The second exception is thrown when an instance is required to have a Serializable interface. The last exception is thrown when control information that was read from an object stream violates internal consistency checks. Finally, the ObjectStreamException exists since the second version of Java (1.1).

The Structure of ObjectStreamException

Constructors

The ObjectStreamException in Java

The ObjectStreamException class is very general and a method can use this exception, in order to enclose all its subclasses. The method can either catch the exception, or throw it to its caller method. In this tutorial we will show you A sample example that throws a ObjectStreamException is shown below:

ObjectStreamExceptionExample.java:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.util.HashMap;
import java.util.Map;

class SampleObject {
	private Map map = null;
	
	public SampleObject() {
		map = new HashMap();
	}
	
	public void addPair(String key, String val) {
		map.put(key, val);
	}
}

public class ObjectStreamExceptionExample {
	
	private final static String OUTPUT_FILE = "out.txt";
	
	public static void execute() throws FileNotFoundException, IOException {
		SampleObject obj = new SampleObject();
		
		// Add some pairs into the map.
		obj.addPair("key1", "val1");
		obj.addPair("key2", "val2");

		ObjectOutputStream out = null;
		
		out = new ObjectOutputStream(new FileOutputStream(OUTPUT_FILE));
		out.writeObject(obj);

		out.close(); //Close the stream.
	}
	
	public static void main(String[] args) {
		try {
			TestJava.execute();
		}
		catch(ObjectStreamException ex) {
			System.err.println("An ObjectStreamException was caught!");
			ex.printStackTrace();
		}
		catch (FileNotFoundException ex) {
			System.err.println("A FileNotFoundException was caught!");
			ex.printStackTrace();
		}
		catch (IOException ex) {
			System.err.println("An IOException was caught!");
			ex.printStackTrace();
		}
	}
}

In this example, we define a SampleObject class, which contains a Map as a private field. The SampleObject class does not implement the Serializable interface and thus, cannot be written to an ObjectOutputStream. The resulting NotSerializableException is caught inside the main method.

A sample execution is shown below:

An ObjectStreamException was caught!
java.io.NotSerializableException: main.java.SampleObject
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
	at main.java.TestJava.execute(TestJava.java:37)
	at main.java.TestJava.main(TestJava.java:44)

 
This was a tutorial about the ObjectStreamException in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of