aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

Ira Jain
Ranch Hand

Joined: Sep 06, 2000
Posts: 70
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

Joined: Sep 05, 2000
Posts: 523

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

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Sep 28, 2000
Posts: 199
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

Joined: Oct 20, 2000
Posts: 3
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

Joined: Sep 29, 2000
Posts: 17
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

Joined: Sep 23, 2000
Posts: 149
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

Joined: Sep 06, 2000
Posts: 70
Thank you for all the helpful replies.It really cleared up my concepts.


------------------
Aru Ven
Ranch Hand

Joined: Sep 28, 2000
Posts: 199
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling
 
Similar Threads
which executes first?
Answer this correctly and dont worry about Exceptions Flow Control
Exception Problem
exception problem:looking at following two code fragments!
About Exception Handling