0

I have a public class that extends Exception. It is my custom Exception class that looks like this:

package com.yahoo.chris511026.paragraphcount;

    public class InputException extends Exception {

    public String getMessage() {

        return "Error: No text entered.";

    }

}

Underneath InputException is a yellow line indicating that the class InputException does not declare a static final version UID field of type long. Why is it saying this if my class doesn't implement the Serialiazble interface? What do I do to clear resolve this issue. I don't want to have to modify or add anything to my code.

Chris Olszewski
  • 153
  • 2
  • 8
  • 3
    possible duplicate of [What does it mean: The serializable class does not declare a static final serialVersionUID field?](http://stackoverflow.com/questions/2288937/what-does-it-mean-the-serializable-class-does-not-declare-a-static-final-serial) – T.J. Crowder Feb 11 '13 at 15:36
  • Your class *does* implement the Serializable interface, by inheritance. – user207421 Feb 11 '13 at 17:06

4 Answers4

4

This is a warning not an error. It is pointing out that it is good practice to add a serialVersionUID field to any serializable class. And your exception class is already serializable because it indirectly extends Throwable and Throwable implements Serializable.

(If you don't define a serialVersionUID field, then the JVM computes one on the fly based on the current API definition of the class. This makes you more vulnerable to problems with serial version UID mismatches due to class changes.)

Why is it saying this if my class doesn't implement the Serialiazble interface?

No. It is not saying that.

What do I do to clear resolve this issue. I don't want to have to modify or add anything to my code.

There are three ways to deal with this:

  • If you don't want to change any code, modify your IDE's compiler settings to disable this warning.

  • Add a serialVersionUID field; e.g. using your IDE's accelerator which should offer you the option of computing it using the standard algorithm.

  • Add an appropriate @SuppressWarning annotation. This would be appropriate if there is zero chance that your exception objects would need to be serialized in the future.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

You extend Exception which itself extends Throwable which implements Serializable.

Update since a bit too much is ending up in comments:

Options:

  • add the id
  • @SuppressWarning("serial")
  • configure your IDE to ignore it
  • ignore it alltogether
Simon Verhoeven
  • 1,295
  • 11
  • 23
0

The Exception class implements the Serializable interface and, through inheritance, so does your class.

The serial version unique id exists so that you can distinguish between versions of the same class when de-serialising it (e.g. if you later want to add a new member to your class that would make it incompatible with older versions without that member).

yiannis
  • 1,421
  • 12
  • 21
0

Because the java exception class implements the Serializable interface . And thus you must declare the static final version UID in the child classe of the class exception.

If you don`t declare it, the program will automatically generate one. This is ok for most situation but may cause problem if two exception classes generate the same UID or if the class change and you happen to desirialize an old exception with the old UID.

Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58