8

Why do we need serialVersionUID when extending RuntimeException? Is RuntimeException a serializable class?

public class DataNotFoundException extends RuntimeException {       
    /**
     * 
     */
    private static final long serialVersionUID = 1;

    public DataNotFoundException(String str)
    {
        super(str);
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
yogesh sharma
  • 101
  • 1
  • 4

3 Answers3

5

RuntimeException extends Exception. Exception extends Throwable. Throwable implements Serializable. So DataNotFoundException is Serializable too

user207421
  • 305,947
  • 44
  • 307
  • 483
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
5

Yes, Throwables are Serializable. Serializable means that objects of that class can be converted into a sequence of bytes, which can be reassembled by another jvm. Making an exception serializable means it can be transferred across the network between tiers of a distributed application.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
1

Exceptions Classes or any classes which implements Serializable would need to have SerialUID, so that class that has do be serialized at the time of compiling, and de-serialized at the time of recompiling, JVM will do version matching which version of serialized class is being de-serialized.
Serialisation is implemented for following reasons.

  1. Support bidirectional communication between different versions of a class operating in different virtual machines by:
    • Defining a mechanism that allows JavaTM classes to read streams written by older versions of the same class.
    • Defining a mechanism that allows JavaTM classes to write streams intended to be read by older versions of the same class.
    • Provide default serialization for persistence and for RMI.
    • Perform well and produce compact streams in simple cases, so that RMI can use serialization.
    • Be able to identify and load classes that match the exact class used to write the stream.
    • Keep the overhead low for non-versioned classes. Use a stream format that allows the traversal of the stream without having to invoke methods specific to the objects saved in the stream.
surendrapanday
  • 530
  • 3
  • 13