Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making an Exception field transient in Java

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).

like image 611
tttppp Avatar asked Dec 09 '22 08:12

tttppp


1 Answers

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.

like image 93
DaveH Avatar answered Dec 11 '22 21:12

DaveH