1

I have been developing in Java since 8 months, and I didn't face a case where I went to serialize an exception, I'm asking because I saw the serialVersionUID and how Eclipse advise to add it explicitly in the Exception class that you define. So, I think that every(or the majority) exception is being serialized and I don't know that, maybe because the framework or the language itself is handling this issue.

I know that serialization is needed when you need to transfer the object through the network or when you need to store it, and I get the idea that we actually serialize the exception when we save the logs/exception backtrace into for example, catalina.out file, am I correct? and what other use cases that may need to serialize the exception?

3
  • Sounds like you got it right. You serialize exceptions for the same reasons you serialize any other object. May 22, 2016 at 18:10
  • @RobertHarvey But why Eclipse is focusing on setting serialVersionUID for exception classes more than other classes? I understand that serialVersionUID is more related to network rather than storing, and I don't see in which case we want to transfer an exception through the network.
    – hex
    May 22, 2016 at 18:18
  • Eclipse enforces setting serialVersionUID in all classes implementing the Serializable interface. Throwable implements Serializable, and so your subclass inherits that. May 24, 2016 at 16:44

1 Answer 1

2

If you're going to serialize a class, it needs a serialVersionID.

Your exception classes should always be serializable. You have no idea where the exception might be used, and if it gets marshaled across an app domain, you may lose debugging information or even lose the entire exception altogether. Exceptions are intended to be usable anywhere, and if they're not serializable, you can't use them across app domains.

The same principles also apply to C# exceptions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.