为什么我的异常类需要序列化?

时间:2021-12-16 20:18:28

When you extend a class with class Exception ( for creating new exception) you get a warning to have a serialVersionUID. I know that serialVersionUID plays an important role while serialization and deserialization, but when my Exception needs to be serialized? Can anyone give me a practical case in which I want my custom-exception class to have serialization and deserialization?

当您使用类Exception扩展类(用于创建新的异常)时,您会收到一条警告,要求它具有serialVersionUID。我知道serialVersionUID在序列化和反序列化时起着重要的作用,但是当我的Exception需要被序列化时?任何人都可以给我一个实际案例,我希望我的自定义异常类具有序列化和反序列化?

3 个解决方案

#1


51  

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.

这是因为所有异常的根类,Throwable实现了Serializable接口。默认情况下,所有异常都是可序列化的,这是一种语言设计决策,因为作者希望异常能够在没有任何特殊配置的情况下通过线路发送。

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.

如果基类不可序列化,那么在远程方法失败的情况下,由于您无法控制内置的异常类型,您将很难传达出现了什么问题。

#2


12  

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.

如果您的自定义异常曾经在分布式应用程序中使用(使用RMI,Spring http-invoker等)并且可以从远程客户端调用的服务器方法抛出,那么必须将异常序列化以跨越线路并去客户端。

#3


4  

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.

您唯一的选择是为您定义的每个Exception类型定义serialVersionUID(IDE可以为您生成它)或禁止警告。

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

您可能会发现我之前的问题显式serialVersionUID被认为有害吗?相关。

#1


51  

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.

这是因为所有异常的根类,Throwable实现了Serializable接口。默认情况下,所有异常都是可序列化的,这是一种语言设计决策,因为作者希望异常能够在没有任何特殊配置的情况下通过线路发送。

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.

如果基类不可序列化,那么在远程方法失败的情况下,由于您无法控制内置的异常类型,您将很难传达出现了什么问题。

#2


12  

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.

如果您的自定义异常曾经在分布式应用程序中使用(使用RMI,Spring http-invoker等)并且可以从远程客户端调用的服务器方法抛出,那么必须将异常序列化以跨越线路并去客户端。

#3


4  

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.

您唯一的选择是为您定义的每个Exception类型定义serialVersionUID(IDE可以为您生成它)或禁止警告。

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

您可能会发现我之前的问题显式serialVersionUID被认为有害吗?相关。