Created
August 28, 2014 12:31
-
-
Save Fhernd/f731268169512604dd0a to your computer and use it in GitHub Desktop.
Demostración del uso de excepciones en threads.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
namespace Recetas.Multithreading.Cap01.R0111 | |
{ | |
public sealed class ManejoExcepcionesThread | |
{ | |
public static void Main() | |
{ | |
Console.Title = "Manejo de Excepciones en Threads ---"; | |
Console.WriteLine (); | |
// Creación de thread con método adyacente que cuenta | |
// con manejo de excepciones interno: | |
Thread t1 = new Thread (ThreadConBloqueTryCatch); | |
// Inicio de ejecución: | |
t1.Start(); | |
// Espera a que finalice para continuar: | |
t1.Join(); | |
// Demostración de ejecución de thread con método adyacente | |
// que no cuenta con su propia implementación para el | |
// manejo de excepción: | |
try | |
{ | |
Thread t2 = new Thread (ThreadSinBloqueTryCatch); | |
t2.Start(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine ("Esta región de manejo de excepción no es alcanzada."); | |
} | |
} | |
// Método que simula el lanzamiento de una excepción, | |
// pero que no cuenta con un bloque de try-catch: | |
private static void ThreadSinBloqueTryCatch() | |
{ | |
Console.WriteLine ("Inicio de ejecución de un thread con fallas."); | |
Thread.Sleep (TimeSpan.FromSeconds (2)); | |
throw new Exception ("¡Falla!"); | |
} | |
// Método que simula el lanzamiento de una excepción: | |
private static void ThreadConBloqueTryCatch() | |
{ | |
try | |
{ | |
Console.WriteLine ("Inicio de ejecución de un thread con fallas."); | |
Thread.Sleep (TimeSpan.FromSeconds (1)); | |
throw new Exception ("¡Falla!"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine ("Control sobre la excepción: {0}", | |
ex.Message | |
); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment