This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes finally block vs normal-out-of-try-catch-scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "finally block vs normal-out-of-try-catch-scope" Watch "finally block vs normal-out-of-try-catch-scope" New topic
Author

finally block vs normal-out-of-try-catch-scope

Ian Cabigon
Greenhorn

Joined: Feb 22, 2010
Posts: 25
Hello,

I'd like to know what is the difference with the following try-catch and try-catch-finally snippets.
Enlighten me please.



Thanks,
Ian
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

If c() is in a finally block, it is executed even if b() throws an exception, or a() throws something that doesn't match the catch block. Without the finally, c() wouldn't execute if either of those things happened.


[Jess in Action][AskingGoodQuestions]
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Thanks Ernest Friedman-Hill


Life is easy because we write the source code.....
Ian Cabigon
Greenhorn

Joined: Feb 22, 2010
Posts: 25
Thank you. I finally got it.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ernest Friedman-Hill wrote:If c() is in a finally block, it is executed even if b() throws an exception, or a() throws something that doesn't match the catch block. Without the finally, c() wouldn't execute if either of those things happened.


additionally, if try has any return statement, then c() without finally fails to execute
 
 
subject: finally block vs normal-out-of-try-catch-scope
 
Similar Threads
IBM Mock Exam: Try Catch Finally ?
exeption handling
Question from 4tests.com
Exception Handling Question from Dan Chisholm's mock exam
Finally block