在 Java 中将异常字段设置为瞬态

发布于 2024-11-05 18:50:53 字数 150 浏览 4 评论 0原文

我有一个扩展 Exception 的类,因此必须是可序列化的。异常类包含一个不可序列化的字段,因此我正在考虑将其设置为瞬态。我的理解是,这样做意味着如果存储我的类,则无法恢复该字段。在执行期间什么时候我的异常可能会被序列化/反序列化? (注意,据我所知,我不会将这些写入数据库或文件)。

I have a class that extends Exception, and therefore has to be Serializable. The exception class contains a field that is not Serializable, so I was considering making it Transient. My understanding is that doing so, will mean that the field cannot be recovered if my class is stored. When during execution might my Exceptions be serialized/deserialized? (n.b. As far as I know, I'm not writing these to a database or file).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟火散人牵绊 2024-11-12 18:50:53

如果您自己没有明确地序列化它们,那么我认为可以安全地假设使您的场瞬态不会产生不良影响。

根据我的理解,如果没有明确要求,JVM 不会序列化对象,因此,除非您的应用程序期望 Exception 类被序列化,否则我认为您没有理由担心。

我想如果您正在编写一个库,因此无法了解您的类的所有用例,那么您可能需要更加小心。

If you are not explicitly serialising them yourself, then I think its safe to assume that making your field transient will have no bad effects.

From my understanding, the JVM does not serialise objects without being explicitly asked to do so, so, unless expect the Exception class to be serialised by your application, I don't think you have cause to worry.

I guess that if you are writing a library, and therefore can't know all the use cases of your class, you may need to be a little more careful.

花期渐远 2024-11-12 18:50:53

如果异常通过 RMI、RPC 框架或类似框架传播,则可能会导致问题。如果您不使用此类功能,则可以将其设置为暂时的。

If the exception is being propagated through RMI, RPC frameworks or similar then it may cause trouble. If you do not use such functionalities, then you can make it transient.

暗恋未遂 2024-11-12 18:50:53

你的理解是正确的。标记为 transient 的字段不会被视为对象状态的一部分,并且会在对象序列化时被有意忽略,并且在再次反序列化对象时不会恢复。当您想要将对象及其状态存储在磁盘上或通过网络发送对象时,就会发生序列化。据我所知,序列化在正常程序执行期间不会发生,除非您明确告诉您的程序这样做。

Your understanding is correct. Fields marked as transient are not considered as part of an object's state and are intentionally left out when the object is serialized, and won't be recovered when the object is deserialized again. Serialization happens when you want to store an object and its state on disk or send an object over the network. As far as I know, serialization doesn't happen during normal program execution, unless you tell your program explicitly to do so..

惟欲睡 2024-11-12 18:50:53

您可以定义 writeObject 来抛出 UnsupportedOperationException 或 NotSerializedException 以确保该对象永远不会被序列化。

You could define writeObject to throw UnsupportedOperationException or NotSerializableException to ensure this object is never Serialised.

扛起拖把扫天下 2024-11-12 18:50:53

是的,如果您的异常中有瞬态字段,那么当您的对象被序列化/反序列化时,瞬态字段将无法恢复。

但是,在小型应用程序中,如果您不自己序列化异常,则不太可能需要序列化/反序列化。但如果您使用任何“神奇”框架或 JNDI 或任何集群环境,请注意。

Yes, if you have transient fields in your exception, the transient fields cannot be restored if your object gets serialized/deserialized.

However, in small applications it is unlikely, that you need serialization/deserialization if you do not serialize the exceptions by yourself. But pay attention if you use any "magic" framework or JNDI or any clustered environment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文