• 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

try/catch with exception casting

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting a runtime 'classCastException' with the following code:
public class examprep
{
public void first() throws Exception
{
System.out.println("Inside the first method!!");
throw new Exception();
}
public void second() throws AWTException
{
try
{
System.out.println("Inside try block of
the second method!!");
first();
}
catch(Exception ex)
{
System.out.println("Inside Exception catch block
of second method!!");
throw (AWTException)ex;
}
}
public static void main(String[] args)
{
examprep ep = new examprep();
try{
ep.second();
}
catch(AWTException ee)
{
System.out.println("Catch block of main method");
}
}
}
I thought that the following line of code would run fine:
throw (AWTException)ex;
since AWTException is a subclass of Exception, why can't I cast Exception to AWTException and throw that result??
Thanks in advance...
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didnot try your code, but the bottom rule is you CANNOT cast a superclass to a subclass. You can only cast a subclass to its superclass.
Hope this helps
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To add to what Ajith has said ... probably in different words....
You don't HAVE to CAST a subclass to its super class. It is, by default, CONVERTED.
Also, you can CAST a REFERENCE of a super class
to a sub-class if the object being referred by this reference
is of sub class type.
Example:
MySuper mSup = new MySub();
MySub mSub;
mSub = (MySub) mSup;
In this code, MySuper is a super class and MySub is a sub-class.
the reference mSup is of type MySuper (super class) while it is
referencing to an object of MySub (sub-class).
The above CAST is valid.
If anyone needs detailed code, please say so ....
Regds.
- satya
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry abt the multiple posts....
I got a server error so I posted this
again ...
Regds.
- satya

[This message has been edited by satya5 (edited May 22, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Satya:
Thanks for the clarification, thats the part that was confusing me. I knew that I had written code that had performed a cast from a superclass to a subclass. The tricky part was realizing that the Super class was in fact a reference to the subclass. Very important difference that is not clearly articulated in most publications.
mySuper = new mySub();
mySub = (mySub)mySuper; --> valid
-------------------------------------
mySuper = new mySuper();
mySub = (mySub)mySuper --> not valid, runtime exception
thanks again
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way I simplify this ordeal of casting ( and reference assignment ) is by thinking about these classes as though they are something real. Believe me, it helps.
To help you begin with, just replace those Base{} , Sub{}, Derived{} names with something more meaningful without losing the relations.
<PRE>
class Fruit{} // better than Base !
class Apple extends Fruit{} // Better than Derived!
class RedApple extends Apple{} // Better than derived2!
class Grape extends Fruit{} // Better than Sub2!
</PRE>
Once you have done this translation in your mind, think about the
choics given.
Can Grape be cast to Apple? Fruit can be Grape,Apple, or RedApple right? How about a RedApple reference being assigne to Apple? What happens at runtime if you say AppleObj=(Apple)GrapeObje?..........
Might sound funny, but with a little practice you will find this a very convenient and quite an enjoyable way to solve the casting issues. After all, this is what Object Oriented programming is all about. A design philosophy that can represent the real world relations.
Enjoy Casting......
Ajith
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic