Una excepción es un error que ocurre durante la ejecución del programa.

try-catch permite manejar algunos errores sin que el flujo termine inmediatamente.

try y catch

try
{
    int numero = int.Parse("hola");
}
catch
{
    Debug.Log("Ocurrió un error");
}

Capturar la excepción

try
{
    int numero = int.Parse("hola");
}
catch (Exception error)
{
    Debug.Log(error.Message);
}

Necesita:

using System;

Excepciones concretas

Podemos capturar únicamente cierto tipo de error.

try
{
    int numero = int.Parse(texto);
}
catch (FormatException)
{
    Debug.Log("El formato no es válido");
}

finally

Se ejecuta haya ocurrido un error o no.

try
{
    Debug.Log("Intentando");
}
catch
{
    Debug.Log("Error");
}
finally
{
    Debug.Log("Fin");
}

throw

Permite lanzar una excepción manualmente.