Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finally block runs twice?

 
jay sugrue
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PLEASE format your code properly. What you posted above it a hot mess, and next to impossible to read.
 
Peter O'Reilly
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21208
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21208
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: Or tying you to the ground in that scene around the campfire in Blazing Saddles.

Now THAT was a sheriff, Sheriff.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic