Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enthuware questions

 
k reeta
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!
 
Sanjeev Ba
Ranch Hand
Posts: 40
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic