This week's book giveaway is in the JavaScript forum.
We're giving away four copies of JavaScript Promises Essentials and have Rami Sarieddine on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes try/catch with exception casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "try/catch with exception casting" Watch "try/catch with exception casting" New topic
Author

try/catch with exception casting

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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...
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks ajith
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Mar 17, 2000
Posts: 5782
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try/catch with exception casting