This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic