• 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

try-catch-finally

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks to all of u. i got the point. satya your example did help me a lot. thanks.
regards
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic