aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Throwbale to Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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: 87
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: 2341
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: 87
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
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: 87
Yes, you're right.
Thanks a lot.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Casting Throwbale to Exception
 
Similar Threads
casting applied to references
Abt Casting...
Casting issues
Casting Object References
type conversion