This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes (basic ) use of  finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "(basic ) use of  finally" Watch "(basic ) use of  finally" New topic
Author

(basic ) use of finally

sudheer singampalli
Greenhorn

Joined: Nov 03, 2003
Posts: 4
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?
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

No. The line following the catch() will only be executed if that specific exception is thrown.


Please ignore post, I have no idea what I am talking about.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
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?
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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 ]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
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!


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
sudheer singampalli
Greenhorn

Joined: Nov 03, 2003
Posts: 4
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

Joined: Nov 03, 2003
Posts: 4
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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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?
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
Your cleanup code will not execute if your catch clause throws an exception.
 
GeeCON Prague 2014
 
subject: (basic ) use of finally