• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ClassNotFoundException

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting confused about the ClassNotFoundException , which is thrown during compilation

this cast
ObjectInputStream Ois= new ObjectInputStream(fis);
myser = (MySerialisation) Ois.readObject(); //MySerialisation is a class and if I do not put inside try block error is thrown

But when I cast

Short s1 = (short)((new Short((short)343)) / (new Short((short)49)));

there is no need for a try block, but both are casts??


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Exceptions are never thrown during compilation - only at runtime.

Can you show a complete program which demonstrates the problem? How exactly is the data written to the file that you are trying to read?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

I think it is unfortunate that the same word “cast” is used for the action applied to a primitive and that applied to a reference type, because they are very different actions.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you define the class "MySerialisation"? When you are trying to typecast the object,check whether the class is defined inside the same class. Hope this helps.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ClassNotFoundException (and associated IOException) are nothing to do with casting, and are part of the signature of the ObjectInputStream.readObject() method. So you have to handle them.
 
Alvin Vafana
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for quick response , I think I partially understand ..after following the links given by Dave, I feel I need to just tinker more but I am almost there..
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done DT working out what the problem was. I missed it entirely, but there are several problems in this thread. The confusion about casting is a completely separate problem.
As for having to declare and handle Exceptions, please read the appropriate Java® Tutorials section. Note there is a section about “controversy”; I can show you sites giving a completely different opinion.
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic