• 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

Question regarding finally block (Devaka, Final Exam, Question 1)

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Given the code above, it asks under which conditions the finally block will run definitely. One of the correct answers is:

"The instance is garbage collected."

Then, the explanation given is:

"The answer is correct because when the thread enters the finally block, the exception object e will be garbage collected."

I think I don't understand the answer (what instance does the answer talk about, the exception object, or the instance that is running this code (assuming this is in non-static context. I also don't understand the explanation given. Can someone clarify this?

Thanks,

Ruben
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code only one instance is created. Exception e

When the execution comes out of catch block(it goes to finally), it becomes eligible for garbage collection.

So.. when the object is eligible for garbage collection..finally will definitly be executed.

Its more like a round-around option
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tricky question
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himalay Majumdar wrote:In the code only one instance is created. Exception e

When the execution comes out of catch block(it goes to finally), it becomes eligible for garbage collection.

So.. when the object is eligible for garbage collection..finally will definitly be executed.

Its more like a round-around option


Himalay,

But I think that's not true, because the exception can be thrown inside the catch block again, or it could even be returned (if this code is inside a method which has a return type which is an exception of that type) via a return statement. In both of those cases the exception is not eligible for GC, yet the code has come out of the catch block and is entering the finally block.

You could also make the exception object eligible for GC inside the catch block, and then call System.exit(0), and in that case the finally block will not be entered.

Maybe Devaka can clarify what he had in mind when he wrote this?

Or maybe you are right and I misunderstood you, let me know if you think that's the case.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You could also make the exception object eligible for GC inside the catch block, and then call System.exit(0), and in that case the finally block will not be entered.

.

Thats right..I dint consider this point. May be this question is made for people like me...
Lets wait for Devaka on this.

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himalay Majumdar wrote:

You could also make the exception object eligible for GC inside the catch block, and then call System.exit(0), and in that case the finally block will not be entered.

.

Thats right..I dint consider this point. May be this question is made for people like me...
Lets wait for Devaka on this.


Devaka will clarify this, I have no doubt. Thanks for discussing this though.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the explanation must mean something different, because the exception object can be definitely garbage collected without finally being run:

 
Sheriff
Posts: 6914
1314
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep there is a confusion on this question. However, this question is already removed from the new version.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Devaka Cooray wrote:Yep there is a confusion on this question. However, this question is already removed from the new version.


Excellent, Devaka! When did the new version come out? I think it's too late for me to do all the exams over again.

Also, please check your private messages shortly.
 
Devaka Cooray
Sheriff
Posts: 6914
1314
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote: When did the new version come out?



Oh! It's not a completely new version, just an update. I'm eventually releasing updates when some errors are found.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's great, thank you. What's the best way to notify you about errors, just post in JavaRanch, or do you prefer private messages?

By the way, I think I posted at least another potential mistake in the past few days, but I'm sure you will get to it as you review the threads.
 
Devaka Cooray
Sheriff
Posts: 6914
1314
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is best if you can post a topic it on JavaRanch. And it is much more better if you can notify me about that topic via a PM.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Devaka Cooray wrote:It is best if you can post a topic it on JavaRanch. And it is much more better if you can notify me about that topic via a PM.


Aye aye, Captain!
 
Devaka Cooray
Sheriff
Posts: 6914
1314
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:

Devaka Cooray wrote:It is best if you can post a topic it on JavaRanch. And it is much more better if you can notify me about that topic via a PM.


Aye aye, Captain!



As far as some lot of busy works, I'm not a regular user of JavaRanch. In some cases, I may not visit here for even several weeks. Therefore it is bit hard to find a topic which is about my simulator, as I couldn't read all the topics. Therefore, if someone can notify about the topic via a PM to me, I would consider it.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Devaka Cooray wrote:

Ruben Soto wrote:

Devaka Cooray wrote:It is best if you can post a topic it on JavaRanch. And it is much more better if you can notify me about that topic via a PM.


Aye aye, Captain!



As far as some lot of busy works, I'm not a regular user of JavaRanch. In some cases, I may not visit here for even several weeks. Therefore it is bit hard to find a topic which is about my simulator, as I couldn't read all the topics. Therefore, if someone can notify about the topic via a PM to me, I would consider it.


Good idea, and I hope everybody else takes notice as well.

Regards,

Ruben
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic