need for "finally" clause

I can't understand the need of finally clause in java.

for eg:

try

{

System.out.println("Try");

float aQuotient = 5 / 0; // (or) float aQuotient = 5 / 2;

}

catch (Exception obj)

{

System.out.println("Catch");

}

finally

{

System.out.println("Finally");

}

can be written as

try

{

System.out.println("Try");

float aQuotient = 5 / 0; // (or) float aQuotient = 5 / 2;

}

catch (Exception obj)

{

System.out.println("Catch");

}

System.out.println("Finally");

both will give the same result. Then whats the exact need for "finally". Plz help me in this.

Thanks

Raji

[733 byte] By [raji_gobia] at [2007-11-27 1:31:24]
# 1

> I can't understand the need of finally clause in

> java.

The difference between the two pieces of code you wrote is that the code in the "finally" clause is guaranteed to execute, while the code outside of that is not.

(did anyone otehr'n me wince when they saw the subject line here?)

tsitha at 2007-7-12 0:34:07 > top of Java-index,Java Essentials,New To Java...
# 2

Write this without finally:

void m() throws IOException {

try {

InputStream in = new FileInputStream(...);

//use in

...

} finally {

in.close();

}

}

DrLaszloJamfa at 2007-7-12 0:34:07 > top of Java-index,Java Essentials,New To Java...
# 3

The finally clause will always be executed except for a few rare conditions. Imagine you have a DB connection open in your try catch and you need to make sure you close it then this is a good example for when to use a finally block. The key is that it will always execute. Events in the catch might cause your system.out never to print.

futureHeada at 2007-7-12 0:34:07 > top of Java-index,Java Essentials,New To Java...
# 4

> Write this without finally:

> > void m() throws IOException {

>try {

>InputStream in = new FileInputStream(...);

>//use in

>...

> } finally {

> in.close();

>

>

Quick question:

Variables defined in a try block only exist for the scope of the try..catch block, correct? Therefore, in the quoted code, wouldn't that code not compile? I remember having some issue like that, but I forgot what happened.

Djaunla at 2007-7-12 0:34:07 > top of Java-index,Java Essentials,New To Java...
# 5

D'oh! You are right. I should have written:

void m() throws IOException {

InputStream in = new FileInputStream(...);

try {

...

//use in

...

} finally {

in.close();

}

}

... which is a common template.

DrLaszloJamfa at 2007-7-12 0:34:07 > top of Java-index,Java Essentials,New To Java...
# 6
Oh I see. I wasn't trying to detract from your post or undermine you or anything, I was just curious. Thanks
Djaunla at 2007-7-12 0:34:07 > top of Java-index,Java Essentials,New To Java...
# 7
I usually compile first, but that time I thought I could just toss it off :-(
DrLaszloJamfa at 2007-7-12 0:34:07 > top of Java-index,Java Essentials,New To Java...
# 8
> I usually compile first, but that time I thought I> could just toss it off :-(Serves you right! It's now obvious which one of us is the Java expert isn't it? Obvious as in: you still are ;-)
Djaunla at 2007-7-12 0:34:07 > top of Java-index,Java Essentials,New To Java...