• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Enthuware questions

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I have understood if a risky method in a try block throws an exception, the rest of the try block is not executed. Instead, the catch/finally blocks are executed. But what about the rest of the code in the same method as the try block.



The output of the above code is 1 4 5, which implies that the code after "finally" is executed.

BUT here,



The output is 13423, which implies that the code after finally is not executed.

What am I missing?

Thanks!
 
Ranch Hand
Posts: 41
Android Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Kathy Sierra book.

A finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not.
Even if there is a return statement in the try block, the finally block executes right after the return statement is encountered, and before the return executes!

Based on the code sample provided, I think the behaviour is right.

Hit if(i==2) -> throw exception, handle exception print "2" and return, but just before returning, execute finally -> print 3.

Hope this clarifies.
 
k reeta
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I completely missed the return statement! Makes sense now, thank you!
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic