• 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

about "Exception" ?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.IOException;
class subEx extends IOException {}
class A{
public static void main(String[]args)throws Exception{
try{
method();
}catch(IOException e){
System.out.println("Main()--1 exception");
}catch(Exception e){
System.out.println("Main()--2 exception");
}
}
static void method() throws IOException{
try{
throw new subEx();
}catch(IOException e){
System.out.println("method()---1 exception");
throw new subEx();//why can't be
//caught in the mehod() and must go to the main ?
}catch(Exception e){
System.out.println("method()---2 exception");
}
}
}
//Why the example's result is:
//method()---1 exception and Main()---1 exception
//I really want to know in the method() ,
//why the second " new subEx()"can't be caught
//in the method()?
[ March 04, 2002: Message edited by: Tu Ran ]
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are wondering why other println() statements were not processed it's because there is only one exception thrown, so only one catch statement will be executed.. if the throw exeception has is-a relationship to the listed parameter in each catch clause then the most "specific" type is executed. In fact if you had reversed the order of your catch statements you will be warned by the compiler that the IOException has already been caught, as shown below

HTH
PS. Check JLS(Language Specification) 14.19.1 Execution of try-catch
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry if it seems like I'm ranting but, please, if you're going to ask a question, ASK A QUESTION! Your initial statements says "This is the output..." To that, I say, "Sure. Looks right to me." From there, we're forced to guess what you really wanted to know. I think Chafule did a good job of that, but I can't be sure that what was answered was what you really wanted to know. So, please, post in the form of a question (kinda like the gameshow Jeopardy, if you've ever seen it ).
And, if it's not too much trouble, please put your code within code tags. You can use the buttons at the bottom under the "Instant UBB Code" section, if you need to post code. It makes formatting and reading code much easier.
Thanks,
Corey
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Tu Ran
In case you are asking why the last catch clause is not able to catch the exception thrown in the first one. The reason is that all the catch clauses guard the inmediate try clause.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic