• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Enthuware questions

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
I completely missed the return statement! Makes sense now, thank you!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!