Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All


A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.


// try-catch-finally
using System;
public class EHClass 
   public static void Main () 
         Console.WriteLine("Executing the try statement.");
         throw new NullReferenceException();

      catch(NullReferenceException e) 
         Console.WriteLine("{0} Caught exception #1.", e); 

         Console.WriteLine("Caught exception #2.");

         Console.WriteLine("Executing finally block.");


Executing the try statement.
System.NullReferenceException: Attempted to dereference a null object reference.
   at EHClass.Main() Caught exception #1.
Executing finally block.

For more information and examples on rethrowing exceptions, see try-catch, 8.10 The try statement and Throwing Exceptions.

See Also

C# Keywords | Compare to C++ | Exception Handling Statements | throw | Throwing Exceptions | C. Grammar

© 2015 Microsoft