35

Syntax aside, what is the difference between

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

and

try {
}
catch() {
}

x = 3;

edit: in .NET 2.0?


so

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

is behaviourally equivalent?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Keshi
  • 377
  • 4
  • 5
  • You might want to specify a programming language. Behavior will vary depending on the language. – Eric Z Beard Sep 08 '08 at 20:44
  • To decide the best answer I think you need to clarify if the try/catch blocks are indeed empty or not. As stated in one of the answers, the semantics is the same if the try/catch blocks are empty, otherwise different answers address the semantic differences. – Kasper Sep 08 '08 at 20:55
  • I agree with previous comments, you need to clarify on actual language and seriously review the answers given. The upmodded answers are either incorrect or incomplete. – Rob Cooper Sep 08 '08 at 21:21

16 Answers16

44

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
  • 6,745
  • 7
  • 38
  • 47
  • 1
    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 Sep 08 '08 at 21:52
  • Everyone here is right. I guess I was going for brevity. I've noticed that the early answers get all the upvotes. ;-) – Josh Hinman Sep 08 '08 at 22:55
  • This was my suspicion, and I appreciate the confirmation. Although it's brief it provides the correct response. – Paul Russell Nov 03 '17 at 15:58
36

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
  • 330,807
  • 53
  • 334
  • 373
  • 2
    I would add to the list of methods which prevent the execution of the finally block the *Environment.Exit* call. – Luca Nov 19 '10 at 22:12
10

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
  • 57,260
  • 24
  • 77
  • 113
9

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

Bartosz Bierkowski
  • 2,782
  • 1
  • 19
  • 18
6

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
  • 11,800
  • 14
  • 41
  • 45
3

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.
  • 15,033
  • 14
  • 47
  • 57
2

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
  • 19,783
  • 6
  • 37
  • 57
  • please define a language, as that is impled in your answer but not stated. – shsteimer Sep 08 '08 at 22:22
  • A stackoverflow will also cause a finally block not to execute. – David Basarab Sep 10 '08 at 15:41
  • 1
    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 Sep 18 '08 at 17:35
1

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
  • 28,607
  • 10
  • 51
  • 67
  • 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 Sep 08 '08 at 21:00
1

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
  • 4,208
  • 21
  • 33
1

@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
  • 7,562
  • 10
  • 27
  • 29
  • 1
    Just a note here the line `throw(e); //rewrites stack trace` is different from `throw; //re-throws the original exception` – vitule Mar 24 '10 at 15:27
1

This is not an answer, but a critique. This question is old, but this has always bothered me. I found it here for a reason. I've read every answer and it looks to me that nobody really thought it through.

I really think there is no good point to finally, which may be why it didn't exist in programming languages until "recently". Most of the examples stating stream.close() can cause null reference exceptions, so you still have to test if it's null.

Yes, if you return from within try{}, finally still runs. But is that good practice? It seems like mental gymnastics, might as well bring goto back. Why not wait, and return after the block? All that finally {} does, is add two or three lines to your code.

Jacob Bruinsma
  • 1,087
  • 1
  • 10
  • 23
0

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
  • 88,451
  • 51
  • 221
  • 321
0

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
  • 5,965
  • 2
  • 37
  • 49
0

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

NotMe
  • 87,343
  • 27
  • 171
  • 245
0

@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#

Community
  • 1
  • 1
crashmstr
  • 28,043
  • 9
  • 61
  • 79
0

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
  • 1,062
  • 1
  • 12
  • 19