nedcomp hosting homepage

Producten en Diensten
Dedicated servers
Datacenter informatie
Partners, resellers
Helpdesk informatie
Technische docs, tools
Support homepage
ASP componenten
Praktische ASP, ASP.NET
Visual route server
Whois (domein gegevens)
Software documentatie
Whitepapers
Zoeken
Nedcomp / algemeen

Zoeken
 

Copyright © Nedcomp Hosting
Telefoon nr :   +31 184 670111
Fax nummer :   +31 184 631384
E-mailadres :   info@nedcomp.nl
 

C# Programmer's Reference  

try-catch-finally

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.

Example

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

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

      catch 
      {
         Console.WriteLine("Caught exception #2.");
      }

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

Output

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 | <_pluslang_the_try.2c_.catch.2c_.and_throw_statements>Compare to C++ | Exception Handling Statements | throw | Throwing Exceptions | C. Grammar