Why my exception class needs to be serialized?

24,944

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
24,944
amod
Author by

amod

I am a software developer with strong desire to learn and work on new technologies and solve complex problems with my solid programming knowledge and analytical skills. Currently, I am a Software Developer at Honeywell, Atlanta. I did Master in Computer Science from Lamar University, USA. I had worked as a Senior R&D Engineer in Nokia Networks for 2+ years and before that, I was a software developer in Persistent Systems Ltd for 2 years. In Nokia Networks I was part of core R&D team which design and implement complex telecommunication related algorithms to optimize the mobile network. While working, along with honing my programming skills, I became adept at analytical thinking, debugging, troubleshooting and team working. My core responsibilities also contain guiding junior developer to complete the task provided to them. I like writing code to make day to day life easier. Apart from coding my hobby is to write Blogs I used to write Blogs on various technology available on internet, games, gadgets, social networking, software, Cloud-based applications. I like reading blogs as well, exploring software products and gadgets. I am also a founding member of website CampFestiva.com which publish college events happening all over India. I have been working hard on this for more than 3+ years to make it what it is now. Campfestiva is a non-profitable attempt to provide information about various opportunities to students in India.

Updated on July 09, 2022

Comments

  • amod
    amod almost 2 years

    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?