• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Try - catch - finally return values (again)

 
Bob Graffagnino
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just when I thought I had this down, I get confused again. Can anyone explain the output of the following code:

The output is:
"The output of t.someMethod() = 1"
[ February 12, 2002: Message edited by: Bob Graffagnino ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, the output is:
catch has assigned i the value 1.
finally has assigned i the value 2.
The output of t.someMethod() = 1
An exception is thrown within the try block, so the catch block will, well, catch it and assign 1 to i. Then, when the return statement is encountered, the value of i is stored somewhere as the return value. Just before returning, the finally block is executed, the assignment of 2 to i has no effect. The finally block ends and the method returns...
HIH
 
Bob Graffagnino
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about that. Pretty sneaky if you ask me!
As always Valentin - thank you.
 
aymen esawey
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
my question here is :its not guaranteed that the catch statement is always executed as mentioned in another thread,
and the compiler knows that in try catch finally block the finally block is always executed even if no exceptions result from try.
so why the compiler didn't say:
"missing return statement"?
i compiled it and i see it runs fine but i'm wondering if there is an explanation .i know there is agood one but i don't know it.
 
SaiPrasad Jukalkar
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am also confused on seeing the above code.

if a function is returning a value, and if there is a try/catch block, sometimes we may have to return a value from each block.
pl correct me if iam wrong.
 
Dave Vick
Ranch Hand
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is smart enought o know all of the possible ways for the method to exit and can check them for proper return values. Inthe example given the code will always throw an Exception so the catch will always be exectued and the method will always return a value as required. To see the compiler check the code look at this:

When compiled the way it is above the compiler will produce an error saying the method does not return a value because with line 1 commented out and line 2 uncommented it will throw an uncaught NumberFormatException, so the exception is thrown then finally executes and at no point does it get to a return statement.
If you uncomment line 1 and comment line 2 it will still give you an error because you have a catch block that does not have a return statement.
So the compiler can figure out all of the exits from your method and test the for correctness - that's why you dont get an error in the original code.
This example uses RuntimeExceptions but the principle will be the same for checked Exceptions too.
hope that helps clear it up for you
 
SaiPrasad Jukalkar
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Dave, i got it, your explaination is great
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic