views:

1629

answers:

16

Syntax aside, what is the difference between

try {
}
catch() {
}
finally {
    x = 3;
}

and

try {
}
catch() {
}

x = 3;

edit: in .NET 2.0?

A: 

So you can clean up any open connections, etc. initialized in the try block. If you opened a connection and then an exception occurred, that exception would not be properly closed. This type of scenario is what the finally block is for.

Ryan Lanciaux
+10  A: 

In Java:

Finally always gets called, regardless of if the exception was correctly caught in catch(), or in fact if you have a catch at all.

SCdF
+23  A: 

Depends on the language as there might be some slight semantic differences, but the idea is that it will execute (almost) always, even if the code in the try block threw an exception.

In the second example, if the code in the catch block returns or quits, the x = 3 will not be executed. In the first it will.

In the .NET platform, in some cases the execution of the finally block won't occur: Security Exceptions, Thread suspensions, Computer shut down :), etc.

Vinko Vrsalovic
+26  A: 

Well, for one thing, if you RETURN inside your try block, the finally will still run, but code listed below the try-catch-finally block will not.

Josh Hinman
Wouldn't consider this the actual answer - more a statement of fact ...
iAn
Seconded! I would say this is not "the answer"..
Rob Cooper
You're right. It is a good reply but I guess it does not directly answer the stated question. Maybe that means I should have asked a better question :)
Keshi
Everyone here is right. I guess I was going for brevity. I've noticed that the early answers get all the upvotes. ;-)
Josh Hinman
A: 

The finally block is supposed to execute whether you caught the exception or not. See Try / Catch / Finally example

Chris Lively
+2  A: 

In the case, that the try and the catch are empty, there is no difference. Otherwise you can be sure, that the finally will be executed.

If you, for example throw a new Exception in your catchblock (rethrow), than the assignment will only be executed, if it is in the finally-block.

Normally a finally is used to clean up after yourself (close DB-connections, File-Handles and the likes).

You should never use control-statements (return, break, continue) in a finally, as this can be a maintenance nightmare and is therefore considered bad practice

Mo
+1  A: 

The finally block will always be called (well not really always ... ) even if an exception is thrown or a return statement is reached (although that may be language dependent). It's a way to clean up that you know will always be called.

Moe
I've always loved the daily wtf article that you linked - especially one of the comments that suggests we need a magicFairyFinally that's guaranteed to execute, even with no power!
Jarrod Dixon
A: 

@Ed, you might be thinking of something like a catch(...) that catches a non-specified exception in C++.

But finally is code that will get executed no matter what happens in the catch blocks.

Microsoft has a help page on try-finally for C#

crashmstr
A: 

The finally block is in the same scope as the try/catch, so you will have access to all the variables defined inside.

Imagine you have a file handler, this is the difference in how it would be written.

try
{
   StreamReader stream = new StreamReader("foo.bar");
   stream.write("foo");
}
catch(Exception e) { } // ignore for now
finally
{
   stream.close();
}

compared to

StreamReader stream = null;
try
{
    stream = new StreamReader("foo.bar");
    stream.write("foo");
} catch(Exception e) {} // ignore

if (stream != null)
    stream.close();

Remember though that anything inside finally isn't guaranteed to run. Imagine that you get an abort signal, windows crashes or the power is gone. Relying on finally for business critical code is bad.

Mats Fredriksson
please define a language, as that is impled in your answer but not stated.
shsteimer
A stackoverflow will also cause a finally block not to execute.
David Basarab
I think the first code sample is incorrect (in C#) -- variables declared in the "try" block are *not* in scope in the catch or finally blocks. I tried this in a test C# application and got a compile-time error on the finally block reference: "The name 'stream' does not exist in the current context".
Jon Schneider
A: 

so

try {
    throw something maybe
    x = 3
}
catch (...) {
    x = 3
}

is behaviourally equivalent?

Keshi
No. It eats the exception, while a finally block would not.
Shog9
Duplicating code is a Very Bad Idea and *will* bite you. Using a finally block lets you put cleanup in ONE place, not two.
David Singer
A: 

Any code in the finally is ran in the even in the event of an unhandled exception. Typically the finally code is used to clean up local declarations of unmanaged code using .dispose().

osp70
+9  A: 

try catch finally is pretty important construct. You can be sure that even if an exception is thrown, the code in finally block will be executed. It's very important in handling external resources to release them. Garbage collection won't do that for you. In finally part you shouldn't have return statements or throw exceptions. It's possible to do that, but it's a bad practice and can lead to unpredictable results.

If you try this example:

try {
  return 0;
} finally {
  return 2;
}

The result will be 2:)

Comparison to other languages: Return From Finally

bibix
Keep in mind the Finally Block is not always executed. A stackoverflow (not pun intended) will cause the finally block not to execute.
David Basarab
The result will be 2 in Java. In C# this code can't be compiled.
thorn
A: 

Finally blocks permit you, as a developer, to tidy up after yourself, regardless of the actions of preceeding code in the try{} block encountered errors, and have others have pointed out this, is falls mainly under the umbrella of freeing resources - closing pointers / sockets / result sets, returning connections to a pool etc.

@mats is very correct that there is always the potential for "hard" failures - finally blocks shouldn't include mission critical code, which should always be done transactionally inside the try{}

@mats again - The real beauty is that it allows you throw exceptions back out of your own methods, and still guarantee that you tidy up:

try
{
StreamReader stream = new StreamReader("foo.bar");
mySendSomethingToStream(stream);
}
catch(noSomethingToSendException e) {
    //Swallow this    
    logger.error(e.getMessage());
}
catch(anotherTypeOfException e) {
    //More serious, throw this one back
    throw(e);
}
finally
{
stream.close();
}

So, we can catch many types of exception, process them differently (the first allows execution for anything beyond the try{}, the second effectively returns), but always neatly and tidily clear up.

iAn
+1  A: 

There are several things that make a finally block useful:

  1. If you return from the try or catch blocks, the finally block is still executed, right before control is given back to the calling function
  2. If an exception occurs within the catch block, or an uncaught type of exception occurs in the try block, the code in the finally block is still executed.

These make finally blocks excellent for closing file handles or sockets.

wvdschel
+1  A: 

@iAn and @mats:

I would not "tear down" anything in finally {} that was "set up" within the try {} as a rule. Would be better to pull the stream creation outside of the try {}. If you need to handle an exception on stream create this could be done in a greater scope.

StreamReader stream = new StreamReader("foo.bar");  
try {
    mySendSomethingToStream(stream);
}
catch(noSomethingToSendException e) {
    //Swallow this    
    logger.error(e.getMessage());
}
catch(anotherTypeOfException e) {
    //More serious, throw this one back
    throw(e);
}
finally {
    stream.close();  
}
Sean
Just a note here the line `throw(e); //rewrites stack trace`is different from `throw; //re-throws the original exception`
vitule
A: 

In Java, you use it for anything that you want to execute regardless of whether you used a "return", just ran through the try block, or had an exception caught.

For example, closing a database session or a JMS connection, or deallocating some OS resource.

I am guessing it is similar in .NET?

Uri