try/catch/finally

Existe um bloco que contém as funções tanto de tratamento de erro como de finalização. A sintaxe é a seguinte:

Veja um exemplo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
public class EHClass
{
      public static void Main ()
      {
            try
            {
                  Console.WriteLine("Executando o try.");
                  throw new NullReferenceException();
            }
            catch(NullReferenceException e)
            {
                  Console.WriteLine("Capturando a exception 1.");
            }
            catch
            {
                  Console.WriteLine("Capturando a exception 1.");
            }
            finally
            {
                  Console.WriteLine("Executando o finally.");
            }
      }
}