The finally block will always be executed. From MSDN:
The finally block is useful for cleaning up any resources allocated in the try block as well as running any code that must execute even if there is an exception. Control is always passed to the finally block regardless of how the try block exits.
Whereas catch is used to handle exceptions that occur in a statement block, finally is used to guarantee a statement block of code executes regardless of how the preceding try block is exited.
By the way, this is the type of question that you can easily test yourself by writing some code, compiling it, and seeing what happens when you execute it.
class Program {
static void Main(string[] args) {
try {
Console.WriteLine("Trying!");
throw new Exception();
}
catch (Exception e) {
Console.WriteLine("Catching {0}!", e.Message);
throw new Exception();
}
finally {
Console.WriteLine("Finally!");
}
}
}
This outputs:
Trying!
Catching Exception of type 'System.Exception' was thrown.!
Unhandled Exception: System.Exception: Exception of type 'System.Exception' was
thrown.
at TestFinally.Program.Main(String[] args) in C:\Documents and Settings\Me\My
Documents\Visual Studio 2008\Projects\TestFinally\TestFinally\Program.cs:line 15
Finally!
Press any key to continue . . .