Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Error while deserializing an object.  RSS feed

 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting following exception, while using readObject() method:

DeSerializeDate.java:11: unreported exception java.lang.ClassNotFoundException;
must be caught or declared to be thrown
d=(Date) s.readObject();
^


Please give solution for it...
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Catch or re-throw ClassNotFoundException.

I'll explain the reason why ObjectInputStream.readObject() can throw this exception with this little example:

You have an instance of class X. You serialize this instance to a file, and then give the file to someone else. This other person does not have class X on his class path. He then tries to de-serialize the file. The de-serialization tries to find class X but it can't find it. It doesn't know what to do so it throws an exception. It could have thrown an IOException but a ClassNotFoundException makes it clearer what the exact problem is - a missing class file.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!