• Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization : Serialize - use IOException, Deserialize - use Exception  RSS feed

 
Kaur Manpreet
Ranch Hand
Posts: 30
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I was studying the Serialization today and I am surprised by one part.

When we serialisze an object the code is written in try/catch and the exception used is IOException.
Whereas when we deserialize an object (code in try/catch once again) the exception used is Exception and not IOException.

I tried to compile the code and it did give me compile time error stating ClassNotFoundException.

I do understand that IOException and ClassNotFoundException are peer exceptions and hence if we use parent exception to handle basically the Exception class in try/catch we are safe here.

Just curious, why is this not true in case of serialization?

Thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaur Manpreet wrote:Hi All,

I was studying the Serialization today and I am surprised by one part.

When we serialisze an object the code is written in try/catch and the exception used is IOException.
Whereas when we deserialize an object (code in try/catch once again) the exception used is Exception and not IOException.


That's a bad example then. The only checked exception thrown by deserializing (ObjectInputStream's methods) is IOException. And it's generally a bad idea to catch Exception (as it almost certainly is in the example you're looking at).

I tried to compile the code and it did give me compile time error stating ClassNotFoundException.


You generally shouldn't catch that one either, as it's an unchecked exception.
 
Kaur Manpreet
Ranch Hand
Posts: 30
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,

Please assist to review the following code snippet:




If Line 1 is uncommented and Line 2 is commented I get ClassNotFoundException.

Also, please correct me ClassNotFoundException seems to be checked exception, its not subclass of RuntimeException.

Thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My mistake. You're right, it's a checked exception.

The way they use catch(Exception e) there is lazy and bad practice. Better to catch IOException and ClassNotFoundException individually.

I think Java 7 even lets you do something like catch (Exception1, Exception2, ..., ExceptionN e) or maybe it's catch (Exception1 & Exception 2 & ... & ExceptionN e), so that you can name individual exceptions, but not have to repeat a bunch of catch blocks if you're doing the same thing for all of them.
 
Kaur Manpreet
Ranch Hand
Posts: 30
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff on the Java 7 update maybe I'll try that one too when I upgrade my compiler
And I really understand that its a good idea to write different handlers for peer exceptions.

1. I am not sure why the ClassNotFoundException is thrown in this case. I am going through the Java API (but a little help would be good here to start with )
2. Just curious, why is this not true in case of serialization?

Thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaur Manpreet wrote:
1. I am not sure why the ClassNotFoundException is thrown in this case.


The serialization stream includes the class names for the objects, but not the class definitions. You have to have the class available on the receiving/deserializing end, which will do a Class.forName() or something like that.

2. Just curious, why is this not true in case of serialization?


Because you obviously have the class you're serializing at the moment of serialization.
 
Kaur Manpreet
Ranch Hand
Posts: 30
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!