Class InvalidClassException in Java

digitization, transformation, binary

Class InvalidClassException in Java

This exception signals that the serialization mechanism has encountered one of several possible problems with the class of an object that is being serialized or deserialized. The classname field should contain the name of the class in question, and the getMessage() method is overridden to return this class name with the message.

An InvalidClassException is thrown during object serialization. It indicates that the run-time
environment does not support a serialized class for one of the following reasons:

Ads code goes here
  • The version of the class does not match the serial version of the class in the stream.
  • The class contains unknown data types.
  • An InvalidClassException can also indicate one of these problems with the class itself:
  • The class implements only one of writeObject() and readObject().
  • The class is not public.
  • The class does not have an accessible constructor that takes no arguments.

The structure of the class InvalidClassException is as follows:

public class java.io.InvalidClassException extends java.io.ObjectStreamException{
// Public Constructors
public InvalidClassException(String reason);
public InvalidClassException(String cname, String reason);
// Public Instance Variables
public String classname;
// Public Instance Methods
public String getMessage(); // Overrides Throwable
}

The details of the class structure are given as:

public InvalidClassException(String reason);

public InvalidClassException(String reason) constructor creates an InvalidClassException with the specified reason string.

Parameter
reason-The reason the exception was thrown.

public InvalidClassException(String cname, String reason);

public InvalidClassException(String cname, String reason) constructor creates an InvalidClassException with the specified class name and reason string.

READ  How To Format Output in Java?

Parameter
cname-The name of the class.
reason-The reason the exception was thrown

public String classname;

public String classname is the name of the class that caused the exception

public String getMessage();

public String getMessage() method returns the reason string for this exception. If a class name has also been specified, it is prepended to the reason string with a semicolon.
This method returns the reason string for this exception.
The class InvalidClassException also inherits methods from class Object and Throwable.

From Object classFrom Throwable class
clone()fillInStackTrace()
getClass()getMessage()
notify()printStackTrace()
wait()printStackTrace(PrintWriter)
wait(long, int)getLocalizedMessage()
wait(long)printStackTrace(PrintStream)
equals(Object)
toString()
finalize()
hashCode()
notifyAll()

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *