为什么我的异常类需要序列化? [英] Why my exception class needs to be serialized?

查看:605
本文介绍了为什么我的异常类需要序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 a 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?

推荐答案

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

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.

这篇关于为什么我的异常类需要序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆