• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

about finally

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
confused about finally,for example:

and before return ,the finally block will execute,and the value of i should be 1,and then return i,output will 1.but the result is 0.cloud someone tell me why?
thanks in advance

[ September 07, 2005: Message edited by: dx wu ]
[ September 07, 2005: Message edited by: Michael Ernest ]
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code doesn't even compile as a[5] within try block is not a statement.

Pl. provide correct code to clear your confusion.
 
dx wu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and have correct the code
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code compiled fine for me. I got this runtime result:

Here's a modification that may help you see what's happening:

Here's the output I got:
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have modified the code a little bit for more clarification.



I have added my comments, but experts please correct me if I am wrong.

Thanks
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not the result I expected either. After playing with the code a bit, I suspect here is what happens...

1)In the try block array element 5 is accessed generating an ArrayIndexOutOfBoundsException.

2)The exception is caught and the "return i" statement is executed in the catch block. The variable "i" at this point has a value of 0 which the JVM stores off as the return value for the function.

3) The finally block is executed. The variable "i" is incremented, but this has no effect on the value returned by the function, because the return statement has already executed in step #2.

4) The method exits with the return value that was saved off by the JVM in step #2.
[ September 08, 2005: Message edited by: Todd Johnson ]
 
dx wu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks everyone very much
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic