wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Throwbale to Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting Throwbale to Exception" Watch "Casting Throwbale to Exception" New topic
Author

Casting Throwbale to Exception

Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
Hi,

As we all know casting super class onject references to subclass references gives ClassCastException.

But casting Throwbale to Exception doesn't give any exception.

why so? is it exception?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Can you explain what you tried?

I tried this simple program, and it gives a ClassCastException.


[ August 23, 2006: Message edited by: Keith Lynn ]
Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
If you add a print statement in the catch block to see the type of exception it is, you will see that it is a ClassCastException.

So the attempt to cast the Throwable object to an Exception causes the ClassCastException. Since ClassCastException is a subclass of Exception, the catch block will catch it.
[ August 23, 2006: Message edited by: Keith Lynn ]
Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
Yes, you're right.
Thanks a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting Throwbale to Exception
 
Similar Threads
casting applied to references
Casting issues
Casting Object References
Abt Casting...
type conversion