Advertisement
Advertisement


What is the point of the finally block?


Question

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?

2011/11/20
1
31
11/20/2011 1:43:08 PM

Accepted Answer

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.

2008/09/08
35
9/8/2008 8:55:23 PM


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.

2008/09/08

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

2008/09/09

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.

2008/09/08

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

2008/09/08

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.

2008/09/08

Source: https://stackoverflow.com/questions/50618
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]