Why my exception class needs to be serialized?

前端 未结 3 1048
半阙折子戏
半阙折子戏 2020-12-24 00:47

When you extend a class with class Exception ( for creating new exception) you get a warning to have a serialVersionUID. I know that serialVersionUID

相关标签:
3条回答
  • 2020-12-24 01:28

    Your only options are to either define serialVersionUID for every Exception type you define (the IDE can generate it for you) or suppress the warning.

    You may find my earlier question explicit serialVersionUID considered harmful? relevant.

    0 讨论(0)
  • 2020-12-24 01:36

    This is because the root class for all exceptions, Throwable implements the Serializable interface. All exceptions by default are serializable and that's a language design decision because the authors wanted exceptions to be capable of being sent across the wire without any special configuration.

    If the base class is not serializable, you would have a difficult time conveying what exactly went wrong in case a remote method failed since you would have no control over the built in exception types.

    0 讨论(0)
  • 2020-12-24 01:42

    If your custom exception is ever used in a distributed application (using RMI, Spring http-invoker, whatever) and can be thrown from a server method that is invoked from a remote client, then the exception will have to be serialized to cross the wire and go to the client.

    0 讨论(0)
提交回复
热议问题