| 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.
|
 |
 |
|
|
subject: Casting Throwbale to Exception
|
|
|