• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

finally clause is almost always executed, even if there is a return statement

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was not expecting this , thought of sending it to all who dont know that

"finally clause is almost always executed, even if there is a return statement in the catch block."

Taken from
.Marcus Green : Question 15

Question 15)
What will be output if you try to compile and run the following code, but there is
no file called Hello.txt in the current directory?.



1) No such file found
2 No such file found ,-1
3) No such file found, Doing finally, -1
4) 0

Ans is 3
[ December 29, 2004: Message edited by: Swapnil Sapar ]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, actually the answer would be "none of the above" because the output that is generated does not contain commas but line breaks

But indeed, finally is executed before a return statement in the try or catch.
Just about the only exception (no pun intended) is System.exit() which will cause immediate termination of the JVM and skipping of the finally blocks.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeroen

But indeed, finally is executed before a return statement in the try or catch.



According K&B's book on Page 238, "the finally block exceutes right *after* the return statement.

So is it *before* or *after*? From the output it looks like the finally block goes before the return statement. I checked errata of the book and there is nothing for this.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect that the K&B (Sierra/Bates) book is implying that the finally block is effectively triggered by the return statement, i.e. no more of the code in that block will execute after the return statement, so execution moves to the code within finally.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Richard Vagner":

Welcome to JavaRanch. Please take a look at our display name policy and edit your display name to something not obviously fictitious. Hint: Giuseppe Verdee would not work either.

As for your comments: well I wouldn't call the K&B statement an error exactly, it's just a bit incomplete. The return -1 statement is read and executed, in the sense that the return value of -1 has been set - but the return isn't fully completed until the finally has also been processed. The way I think of it, part of the return -1 has been executed, but not all of it. Consider this alternative:

Here the return value is 0, not -1. That's because the return 0 is executed after the return -1. Think of it this way - when a return statement is executed, the JVM saves the return value (as the value that the JVM plans to return, if it doesn't receive other instructions in a finally block). A return statement always is subject to being countermanded later in a finally block. If the finally block doesn't have another return statement, or throw some other exception, then after the finally block executes, the JVM will return whatever value it saved back when the original return was first executed.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic