| title | ms.date | ms.prod | ms.technology | ms.topic | f1_keywords | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | translation.priority.ht | |||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
try-finally (C# Reference) |
2015-07-20 |
.net |
|
article |
|
|
|
c27623fb-7261-4464-862c-7a369d3c8f0a |
25 |
BillWagner |
wiwagn |
|
try-finally (C# Reference)
By using a finally block, you can clean up any resources that are allocated in a try block, and you can run code even if an exception occurs in the try block. Typically, the statements of a finally block run when control leaves a try statement. The transfer of control can occur as a result of normal execution, of execution of a break, continue, goto, or return statement, or of propagation of an exception out of the try statement.
Within a handled exception, the associated finally block is guaranteed to be run. However, if the exception is unhandled, execution of the finally block is dependent on how the exception unwind operation is triggered. That, in turn, is dependent on how your computer is set up. For more information, see Unhandled Exception Processing in the CLR.
Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement. Alternatively, you can catch the exception that might be thrown in the try block of a try-finally statement higher up the call stack. That is, you can catch the exception in the method that calls the method that contains the try-finally statement, or in the method that calls that method, or in any method in the call stack. If the exception is not caught, execution of the finally block depends on whether the operating system chooses to trigger an exception unwind operation.
Example
In the following example, an invalid conversion statement causes a System.InvalidCastException exception. The exception is unhandled.
[!code-cscsrefKeywordsExceptions#4]
In the following example, an exception from the TryCast method is caught in a method farther up the call stack.
[!code-cscsrefKeywordsExceptions#6]
For more information about finally, see try-catch-finally.
C# also contains the using statement, which provides similar functionality for xref:System.IDisposable objects in a convenient syntax.
C# Language Specification
[!INCLUDECSharplangspec]
See Also
C# Reference
C# Programming Guide
C# Keywords
try, throw, and catch Statements (C++)
Exception Handling Statements
throw
try-catch
How to: Explicitly Throw Exceptions