• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

(basic ) use of finally

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why do we need the 'finally' keyword in Java?
irrespective of whether or not an exception is thrown, the statements following 'catch' will be executed, right?
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. The line following the catch() will only be executed if that specific exception is thrown.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sudheer --
You can use "finally" without catch, too, and in fact it's probably more common that way:

Now that close() gets calls whether an exception was thrown, or not. Without finally, you'd have to write two separate "close()" calls to cover those two cases.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One further question. I have included an excerpt of code below:
public class Final_Test
{
public static void main(String[] args)
{
try {
System.out.println("First Line");
System.exit(0);
}
finally {
System.out.println("Finally Line");
}
}
}
I thought that the finally clause would execute before the application exited but discovered that it does not as I understood the finally clause to always execute regardless of what occurs. Is this statement true in all cases except for the System.exit() method invocation? Are there additional cases similar to this?
 
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" clause will always execute except in the case of a "System.exit()".
Even as nasty an error as "java.lang.OutOfMemoryError" will transfer to the "finally" clause, though you may not be able to do much once you get there.
[ November 07, 2003: Message edited by: Wayne L Johnson ]
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.exit(0) does just that - the program exits, terminates, dies, goes to meet its maker, pushes up daisies, whatever. And it does so immediately, without running any finally() clauses or any other code.
Have a great weekend, JavaRanchers!
 
sudheer singampalli
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for that.
could you give me an example where i can see the value/need of 'finally'.
i get a feeling that we can do without the keyword 'finally'.
are the following (section one and section two) not equivalent?
//section one //section two
try { try {
// some statements } // some statements }
catch { //code catch { //code
} }
finally // no-'finally'
{ {
// some clean-up code } // some clean-up code }

i know "section two" above won't compile if there's no 'catch'.
but in that case, can i just not use a 'throws'?
in any case, the clean-up code will compile, right?
 
sudheer singampalli
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, i didn't expect the two columns of my code to mix-up so badly.
i made a mess of it.
plz read the two sections viz. section one and section two above as:
//section one
try {
// some statements }
catch { //code
}
finally
{
// some clean-up code }

//section two
try {
// some statements }
catch { //code
}
// no-'finally'
{
// some clean-up code }
thanks.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sudheer--
You didn't look at my example with FileReader.close() carefully enough. Note that the "close()" in the finally block will be executed whether or not read() throws an exception. Without "finally" you'd have to remember to write the close() in two different places. Generally, duplicate code is a bad thing, yes?
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your cleanup code will not execute if your catch clause throws an exception.
 
reply
    Bookmark Topic Watch Topic
  • New Topic