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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "try-catch-finally " Watch "try-catch-finally " New topic
Author

try-catch-finally

Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277

Hi,
I have some doubts about try-catch-finally statement.
Try {

//code that may throw exception or may not
return 1
}
catch ( XXXException e)
{
//some code
return 2
}
finally {
//some code
return 3
}

return 4
// more code
What is the value of return if ?
1. There is a exception thrown by try block and there is one catch block there
2. There is a exception thrown by try block but no catch block
3. There is no exception
please some one explain it to me.
vivek
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
1) If an Exception is thrown any code in the try that follows the line of code where the exception is thrown will not be executed. If the Exception is the same type as listed in the catch or a subclass of the type, the code in the catch will be executed, the code in the finally will be executed and lastly, any code following the try/catch/finally will be executed(unless there was a return statement in the catch or finally block then only the catch and finally will be executed).
2 If the exception isn't the same type or a subclass, the code in the catch statement(s) will not be executed, the code in the finally block will be executed, the code following the try/catch/finally will NOT be executed and the exception will be thrown by the method as it exits!
3 All the code in the try block, finally block and after the try/catch/finally will be executed. If there is a return in the try block, code in the try block will be executed to that point AND the code in the finally block will be executed.
Note: the only reason I've heard of so far that prevents the code in the finally block from executing is a System.exit().
Looking at the code in your example, it will not compile because the code after the try/catch/finally is unreachable. If you comment it out and get a good compile, you always get 3 returned because finally code block is always the last run in all conditions.
[This message has been edited by Carl Trusiak (edited June 20, 2000).]


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

to add to what carl has said....more practically, use the code
below and verify the output. Run the code without arguments,
with more than one argument and with exactly one argument.
The exceptions thrown are just something which I could remember
off the top of my head, so I used them...
enjoy ...

ps: all names are metaphorically used and do not correspond
to anything living or otherwise
Regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Paul Smiley
Ranch Hand

Joined: Jun 02, 2000
Posts: 244
I don't believe that the code will compile - of course, "Try" is not a keyword ("try" is). But over and above that, finally will (almost - except when there is an OutOfMemory Exception or something similar) always execute except when code calls System.exit(). In your code, you have one path that has two return methods in it - in the try (or catch if an exception is thrown) and in the finally. You will probably get some type of compiler message saying "code not reachable" or something to that effect.
[This message has been edited by Paul Smiley (edited June 20, 2000).]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
You will probably get some type of compiler message saying "code not reachable" or something to that effect.
Paul: I know you are refering to Viveks' code, but I wanted
to add a small correction to what you said.
Nope, the return code in the finally block will overwrite the
previous return code that was set.
In my example above, I commented out the return code in the
finally block so that I could return the string from the
try/catch block.
Regds.
- satya
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi,
May be I am stupid enough but still I don�t understand properly. What does it basically do when it encounter any return statement ?
Let�s take my problem again with some changes
1. Try {
2. System.out.println(�vivek�);
3.//code that may throw exception or may not
4.return 1 }
5.catch ( XXXException e){
6.System.out.println(�vivek in catch�);
7.return 2 }
8.finally {
9. System.out.println(�vivek in catch�);
10return 3 }
11. return 4
What is the value of return if ?
1. There is a exception thrown by try block and there is one catch block there
2. There is a exception thrown by try block but no catch block
3. There is no exception
4.
Please some one explains the sequence of code is going to be executed in each case.
I know this will help me lot.
vivek
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Looking at the code in your example, it will not compile because the code after the try/catch/finally is unreachable. If you comment it out and get a good compile, you always get 3 returned because finally code block is always the last run in all conditions.
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi,
Thanks to all of u. i got the point. satya your example did help me a lot. thanks.
regards
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: try-catch-finally
 
Similar Threads
Try, Catch, Finally
exception
return statement in try block
try catch exception handling
Exceptions Execution flow