try-finally (C# Reference)
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.
In this example, there is one invalid conversion statement that causes an exception. When you run the program, you get a run-time error message, but the finally clause will still be executed and display the output.
// try-finally
using System;
public class MainClass
{
static void Main()
{
int i = 123;
string s = "Some string";
object o = s;
try
{
// Invalid conversion; o contains a string not an int
i = (int)o;
}
finally
{
Console.Write("i = {0}", i);
}
}
}
The example above causes System.InvalidCastException to be thrown.
Although an exception was caught, the output statement included in the finally block will still be executed, that is:
i = 123
For more information on finally, see try-catch-finally.
For more information, see the following sections in the C# Language Specification:
-
5.3.3.14 Try-finally statements
-
8.11 The try statement
-
16 Exceptions
http://msdn.microsoft.com/en-us/library/ms235315.aspx
- 1/3/2011
- Chris Ahlstrom
I have always been under the impression that the goto statement was inherently dangerous because it allowed programs to jump to places in the code, potentially other functions. This obviously makes the program's state non-persistent, being that a goto from function A into function B will cause the stack to no longer be valid, considering the function's local variables. Unlike goto, return maintains persistency of the program's state, no matter where return is used in the function. I have dealt with programming where superiors dictated that return should not be used anywhere but the end of the function, this created an inordinate amount of cruft.
On the other hand, the "finally" clause seems to make little sense, since one could implement it simply by having the code after the try-catch block. If the code should not run, then the exception should be thrown past it, or rethrown if it needs some handling in the local function. It is true that the "finally" clause is not needed, but I would advise just eradicating it.
- 10/4/2007
- Captain Debug
- 6/5/2010
- Thomas Lee
- 8/22/2008
- i-b
- 6/5/2010
- Thomas Lee
In structural programming a return sentence is the last thing we put in the method (the last command the method should preform).
multiple returns are not part of the stuctural traditional programming (just like "goto" is somthing we wish to avoid).
If while programming you discover the need to use a finally block it means that your method is not structural enough.
(You either use return not as last sentence in your method or multiple returns.)
The existance of finally block drives programmers to program in non-structural fashion.
I would advise to revise "finally" into "otherwise" so if an error cought do what is in the catch-block otherwise do what is in the "otherwise".
- 8/18/2007
- Gil1975