Syntax aside, what is the difference between
try {
}
catch() {
}
finally {
x = 3;
}
and
try {
}
catch() {
}
x = 3;
edit: in .NET 2.0?
Syntax aside, what is the difference between
try {
}
catch() {
}
finally {
x = 3;
}
and
try {
}
catch() {
}
x = 3;
edit: in .NET 2.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.
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.
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.
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.
The finally block is supposed to execute whether you caught the exception or not. See Try / Catch / Finally example
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
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.
@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#
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.
so
try {
throw something maybe
x = 3
}
catch (...) {
x = 3
}
is behaviourally equivalent?
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().
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
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.
There are several things that make a finally block useful:
These make finally blocks excellent for closing file handles or sockets.
@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();
}
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?