Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling

 
Ira Jain
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could somebody please explain the output of the following code?

The given output is 1,4 but shouldn't it be 1,4,5.
Ira
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You and I must be on the same page. I just came upon the exact same problem in the Java Certification book. I was thinking the exact same thing you were, however I believe what is happening here is that since a RuntimeException is being thrown the program is forced to exit, so the System.out.println("5"); is never reached. However, the finally block always has to be executed, no matter what. Someone correct me if I am wrong, but RuntimeExceptions cannot be caught. Is this correct?
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The runtime exeception can be caught, but in this example you are not catching it. The exact output is:
1
4
Exception in thread "main" java.lang.RuntimeException
What is happening is that you catch the InterruptedException that you threw, and you catch it in the first try{} block. Then in the catch statement for InterruptedException you throw a new exception, but this is not in a try{} block, so it is not caught. Barry, you are right when you say that 1 gets printed since this is caught, but then an error occurs since the RuntimeException is not caught, so it kicks out to the finally block ("4") and then the program terminates.
[This message has been edited by bill bozeman (edited October 24, 2000).]
 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Can anyone tell me why the Runtime Exceptime thrown in the first catch statment not caught in the immediate next Catch. I mean....
try {
f();
}catch(InterruptedException e){
System.out.println("1");
throw new RuntimeException(); // Why is this not caught
}catch(RuntimeException e){ // here???
System.out.println("3");
return;
}catch(Exception e){
System.out.println("3");
}finally{
System.out.println("4");
}
System.out.println("5");
}

Thx in Advance
Aruna
 
Edy, Yu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aru:
I think the RunTimeException wasnot thrown by f() method. And the try/catch block is trying the catch the exceptions thorwn by f() method.
 
Gautam Pandya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aru:
Your question has already been answered by Bill. The Runtime Exception thrown in the catch block is not caught because it is not within any try block. Any exception will be caught by the surrounding try block and the caught exception will be handled by the catch/finally or both blocks associated with that try block.
Originally posted by Aru Ven:
Hi All,
Can anyone tell me why the Runtime Exceptime thrown in the first catch statment not caught in the immediate next Catch. I mean....

Aruna

 
Prasad Ballari
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aru,
If you throw an Exception from inside another Exception,the JVM passes the Exception call to the next higher level try & catch block.It will never be caught in the same try & catch block untill otherwise its being enclosed with try & caught block.
Following is code which may give u a better idea
CASE 1:

CASE 2: (here o/p is 1,RC,4,5)

Regards
Prasad
[This message has been edited by Prasad Ballari (edited October 24, 2000).]
I added UBB code here. -Mapraputa
[This message has been edited by Mapraputa Is (edited October 24, 2000).]
 
Ira Jain
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all the helpful replies.It really cleared up my concepts.


------------------
 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edy, Gautam, Prasad,
guys Thank You Very much for clearing my doubt....
Aruna
[This message has been edited by Aru Ven (edited October 25, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic