The Artima Developer Community
Sponsored Link

Java Answers Forum
Exception Handling

1 reply on 1 page. Most recent reply: Jan 18, 2004 2:59 AM by Jonathon Brozny

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
suneetha

Posts: 4
Nickname: venussun
Registered: Nov, 2003

Exception Handling Posted: Jan 16, 2004 2:10 PM
Reply to this message Reply
Advertisement
hi
please hlep me with this exception handling

class colorexception extends Exception{}
class whiteexception extends colorexception{}
class blueexception extends whiteexception{}
 
class white{
 
void m1 throws whiteexception(){
throw new blueexception();
}
class a{
public static void main(String args[]){
white w=new white();
try{
w.m1();
}
catch(blueexception e)
{
System.out.println("caught the blue exception");
}
}
}
	

So when I run the above program its poses a compiletime error.And its not catching the blueexception.

if i include multiple catch blocks for the above program..(i mean multiple catch blocks for the try block in the main
method) like
catch(whiteexception e){
System.out.println("caught the whiteexception)}

or any super classes of it
it is perfectly catching the exception and its printing caught theblue exception
Why is it so..??please explain me.

thanking u
Suneetha


Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: Exception Handling Posted: Jan 18, 2004 2:59 AM
Reply to this message Reply
It is throwing a compile time error because of the way java deals with casting.

maybe this will explain it.
public void something(){
	List list = getArrayList();
	ArrayList list2 = (ArrayList)getArrayList();
}
 
public List getArrayList(){
	ArrayList list = new ArrayList();
	return list;
}


the ArrayList is getting put into a list when it leaves getArrayList. So in something() putting it into a list is fine (upcasting is always safe). This is what you're doing with your exception. You are throwing a blueexception upcasting it to a whiteexception in m1().

So the second time I get the ArrayList I try to place it into an ArrayList but I get a compile time error because I am downcasting, in which case I have to cast it.

So you are getting a compile time error for the same reason. You cast it to a whiteexception in which case you have to catch that or a super class of it.

Jonathon

Flat View: This topic has 1 reply on 1 page
Topic: Convert in java string to UCS-2 format for saving in DB Previous Topic   Next Topic Topic: Polutating JTable

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use