This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Finally block runs twice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finally block runs twice?" Watch "Finally block runs twice?" New topic
Author

Finally block runs twice?

jay sugrue
Greenhorn

Joined: May 29, 2012
Posts: 20


The result of this code is
-ic mc mf of

what i can't understand here is that the finally block runs twice - how is this ? I thought a finally code could only run once - is this because they are tied into nested try blocks ? is it that because one try block is nested it can run finally completely independent of the enclosing try block, so both finally blocks run ? or am i in the wrong ball park altogether ?

My thanks in advance.

PS Where i'm located over the next couple of days means at best i'll only have intermittent internet access, so forgive if an immediate reply isn't received (probably won't be able to get back online until 5/6 this afternoon/evening.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Finally block is always executed. It's not that it's executed twice in your code, but you have two finally blocks and each is executed once.


The quieter you are, the more you are able to hear.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

PLEASE format your code properly. What you posted above it a hot mess, and next to impossible to read.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Peter O'Reilly
Greenhorn

Joined: Oct 30, 2012
Posts: 2
fred rosenberger wrote:PLEASE format your code properly. What you posted above it a hot mess, and next to impossible to read.


ha ha....

I could be mistaken but I think that question is taken directly from one of the SCJP exam sample questions and that is exactly how it is formatted.... pure evil...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
Welcome to the Ranch

If that is an exam question, we might have to delete this thread, but not if it is a publicly‑available sample. A lot of questions use such poor code style to try and confuse readers. It appears to work

Jay Sugrue, please tell us where the question comes from.
Peter O'Reilly
Greenhorn

Joined: Oct 30, 2012
Posts: 2
Peter O'Reilly wrote:
fred rosenberger wrote:PLEASE format your code properly. What you posted above it a hot mess, and next to impossible to read.


ha ha....

I could be mistaken but I think that question is taken directly from one of the SCJP exam sample questions and that is exactly how it is formatted.... pure evil...


my apologies I should have worded my reply more carefully. I meant to say the "SCJP exam preparation book" being one of the questions on flow control. I think it is the Kathy Sierra, Bert Bates Text.

jay sugrue
Greenhorn

Joined: May 29, 2012
Posts: 20
thanks everyone for your replies. Peter, it is that ocjp preparation book, i'm going through the self test questions and finding some very tricky so i hoped to get some help on the forum here. is it ok to put them up ? i have four/five more i'm having trouble with. ideally, is there a thread on which these questions have already been discussed ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Peter O'Reilly wrote:
fred rosenberger wrote:PLEASE format your code properly. What you posted above it a hot mess, and next to impossible to read.


ha ha....

I could be mistaken but I think that question is taken directly from one of the SCJP exam sample questions and that is exactly how it is formatted.... pure evil...


That may be true, but it only takes a minute to format it. And it is actually a good habit to get into...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
Thank you for telling us the origins.

I don’t know whether there are threads about those questions, but you can try putting part of the question into our search box. You probably will find something, because lots of people use those books and are bound to have had the same problems. Remember to include the forum named after the exam (maybe this one?) in the search options, because that is where you are most likely to find something.
Yes, you can post your question, but be sure to acknowledge the origin of the question.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

jay sugrue wrote:thanks everyone for your replies. Peter, it is that ocjp preparation book, i'm going through the self test questions and finding some very tricky so i hoped to get some help on the forum here. is it ok to put them up ? i have four/five more i'm having trouble with. ideally, is there a thread on which these questions have already been discussed ?


As long as you quote your sources (and a little better than what you did so far), it should not be an issue. See this link for full details....

http://www.coderanch.com/how-to/java/QuoteYourSources

Henry
jay sugrue
Greenhorn

Joined: May 29, 2012
Posts: 20
Thanks for the replies Campbell and Henry, I'll do that and Henry, yes, I see now what you mean about the formatting. Forgive me, this is what I've been looking at for a few months now and I've no previous experience in Java or any other programming language. I actually think it looks nice and neat that way, ha ! But yeah I'll format in future.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
I'd be very surprised if either a real SCJP question or a sample from K&B did not have at least basic indentation.
Paul Witten
Ranch Hand

Joined: Oct 10, 2012
Posts: 86
Henry Wong wrote:That may be true, but it only takes a minute to format it. And it is actually a good habit to get into...

That original formatting would get somebody shot at a real ranch.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
Paul Witten wrote: . . . That original formatting would get somebody shot at a real ranch.
Disagree. Shooting is too quick and painless. Roasting over a slow campfire, possibly. Or tying you to the ground in that scene around the campfire in Blazing Saddles.
Paul Witten
Ranch Hand

Joined: Oct 10, 2012
Posts: 86
Campbell Ritchie wrote: Or tying you to the ground in that scene around the campfire in Blazing Saddles.

Now THAT was a sheriff, Sheriff.


 
GeeCON Prague 2014
 
subject: Finally block runs twice?