why Java declared serialVersionUID in Exception and Throwable class?

when I created one custom exception class just like below

public class MyApppException extends Exception {

    private String message = null;

    public MyApppException() {
        super();
    }
    public MyApppException(String message) {
        super(message);
        this.message = message;
    }
    public MyApppException(Throwable cause) {
        super(cause);
    }
}

compiler giving me below waning

The serializable class InsufficientBalanceException does not declare a static final serialVersionUID field of type long

From Java doc I understand the meaning of serialVersionUID

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

But I did not understand why Java declared serialVersionUID in Exception and Throwable class? What is the use of serialVersionUID in Exception and Throwable class? Is it really required? If yes, Why? Someone please clarify.

Exception

public class Exception extends Throwable {
    static final long serialVersionUID = -3387516993124229948L;

Throwable

public class Throwable implements Serializable {
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -3042686055658047285L;

Your customized Exception can have a state (=instance variables) they can be additional information that you wish to record with the exception. The state is what gets serialized. and you can change the state when you create a new version of your application (for exmaple, adding new variables) so a class that loads a serialized Exception, needs to know if the version that it loads is compatible with the class definition that the JVM has.

链接地址: http://www.djcxy.com/p/4082.html

上一篇: 为什么java中的serialVersionUID必须是static,final和long类型的?

下一篇: 为什么Java在Exception和Throwable类中声明serialVersionUID?