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

Finally block

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the below example is finally block executed as there are return statements in try & catch block ?
eg:
try
{
return a;
}
catch (Exception e)
{
return b;
}
finally
{
return c;
}
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java's finally block provides a mechanism that allows your method to clean up after itself regardless of what happens within the try block. Use the finally block to close files or release other system resources.
 
SJ Rao
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in this case, is the finally block executed as there are return statements in try / catch block ? Does it still execute finally block ?
This was one of my interview questions.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The finally block is guaranteed to execute regardless of what happens in the "try{ }" and "catch{ }" blocks. The only exception is if the application shuts down, either with an unrecoverable error (Java out-of-memory exception) or an explicit "System.exit()" call.
Try the following code:

The output in both cases is "3", meaning that the "finally" block was executed last and it's return was honored. This will give you a warning message when you compile it, but it does compile and run. The first call to "ugly" throws an exception (Integer divide by zero) and ends up in the "catch" block. The second call to "ugly" works correctly. But both "deferred" to the "finally" block's return.
Needless to say, this is *ugly* programming of the highest order and should be avoided at all cost.
As a general note, you can always figure out what Java actually does by writing a simple program and seeing what the results are ;-)
[ October 29, 2003: Message edited by: Wayne L Johnson ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic