what's the point in finally?

C

Crouchez

kaldrenon said:
are you sure? whether code throws an exception/error/throwable or not it
will still get to //do something in both scenarios. With
StackOverflowError
or OutofMemoryError then the program often stops completely.

public class Test
{
public static void main(String[] args)
{
try
{
return;
}
catch(Exception e){}
finally
{
System.out.println("testing");
}
}
}

Output:
"testing"

yeah i only figured that after i did a test
 
S

steve

try{
//blah }
catch(Exception ex){
// }
finally{ //do something }why not justtry{//blah}catch(Exception
e){}//do something


finally IS ALWAYS CALLED.

Exception is not
 
J

Joshua Cranmer

steve said:
finally IS ALWAYS CALLED.

Exception is not

From Peter Norvig's Java IAQ (Infrequently Answered Questions):

Q: Finally is always called, right?
A:

public class Foo {
public static void main(String[] args) {
try {
System.exit(0);
} finally {
System.out.println("Oi!");
}
}
}

I suppose the best way to phrase that is "A finally block is always
called except if the VM is in a highly abnormal, unrecoverable state
(e.g., SIGKILL) or ceases to exist in the middle of the referent try
block (catch block if one is executed)." ^C'ing an infinite loop also
classifies as an abnormal, unrecoverable state, as does deadlocks.
 
B

bugbear

Joshua said:
steve said:
finally IS ALWAYS CALLED.

Exception is not

From Peter Norvig's Java IAQ (Infrequently Answered Questions):

Q: Finally is always called, right?
A:

public class Foo {
public static void main(String[] args) {
try {
System.exit(0);
} finally {
System.out.println("Oi!");
}
}
}

I suppose the best way to phrase that is "A finally block is always
called except if the VM is in a highly abnormal, unrecoverable state
(e.g., SIGKILL) or ceases to exist in the middle of the referent try
block (catch block if one is executed)." ^C'ing an infinite loop also
classifies as an abnormal, unrecoverable state, as does deadlocks.

I guess the a pickaxe through the CPU die would also
prevent "finally" being called.

I don't normally worry about such things when coding :)

BugBear
 
M

manivannan.palanichamy

try{
//blah }
catch(Exception ex){
// }
finally{ //do something }why not justtry{//blah}catch(Exception
e){}//do something

try
{
//do task1
}
catch(Exception e)
{
handle exception()
}
// do task2

Assume that you need to execute task1. Irrespective of the task1's
success or failure, you need to execute task3. In the above case, no
prob, you dont need to put finally block. task3 will be always
executed though task1 succeeds/fails. Because catch block handles
'Exception', which is the root class of exception hierarchy. So, the
catch block will catch all Exception.

But, look at this code,

try
{
//task1
}
catch(CustomException e)
{
}
finally
{
//task3
}

In this example, the catch block will handle only your
CustomException. Suppose, if any other exception is thrown (say
RuntimeException), then catch block cant handle it, so the execution
will break, and task3 will not be executed. Now, if you put the task3
code inside finally block, it will be always executed irrespective of
any exception.
Generally,
1. logging code will be put in finally block
2. resource free-up code like database connection release, file close
will be present in finally block
3. any acknowledgment message sending code can be placed in finally
block
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top