using System; using System.Threading; using System.Threading.Tasks; namespace Recetas.Multithreading.R0407 { public class GestionExcepcionesTarea { public static void Main() { Console.WriteLine (); // Creación y ejecución de tarea sincrónica: Task<int> tarea1; // Gestión de la excepción producida por `tarea1`: try { tarea1 = Task.Run( () => Tarea("Tarea No. 1", 2)); int resultado = tarea1.Result; Console.WriteLine ("Resultado de Tarea No. 1: {0}.", resultado); } catch (Exception ex) { Console.WriteLine ("Excepción producida en Tarea No. 1: {0}.", ex); } Console.WriteLine ("------------------------------------------------"); Console.WriteLine (); Task tarea2 = new Task<int>(() => Tarea("Tarea No. 2", 3)); Task tarea3 = new Task<int>(() => Tarea("Tarea No. 4", 2)); // Tarea compuesta: Task tareaCompuesta = Task.WhenAll (tarea2, tarea3); tareaCompuesta.ContinueWith (t => Console.WriteLine ("Excepción de `tareaCompuesta`: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted); // Inicio de la ejecución de `tarea2` y `tarea3`: tarea2.Start(); tarea3.Start(); // A espera de la finalización de la ejecución de las tareas: Thread.Sleep (TimeSpan.FromSeconds(5)); } public static int Tarea(string nombre, int duracion) { Console.WriteLine("La tarea `{0}` se está ejecutando en el ID de Thread {1}. " + "¿Thread en pool de threads?: {2}", nombre, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); // Simulación de ejecución tardía: Thread.Sleep (TimeSpan.FromSeconds(duracion)); // Lanzamiento de excepción: throw new Exception("¡Boom!"); return 42 * duracion; } } }
Standard input is empty
La tarea `Tarea No. 1` se está ejecutando en el ID de Thread 4. ¿Thread en pool de threads?: True Excepción producida en Tarea No. 1: System.AggregateException: One or more errors occurred. ---> System.Exception: ¡Boom! at Recetas.Multithreading.R0407.GestionExcepcionesTarea.Tarea (System.String nombre, Int32 duracion) [0x00000] in <filename unknown>:0 at Recetas.Multithreading.R0407.GestionExcepcionesTarea.<Main>m__0 () [0x00000] in <filename unknown>:0 at System.Threading.Tasks.Task`1[System.Int32].InnerInvoke () [0x00000] in <filename unknown>:0 at System.Threading.Tasks.Task.Execute () [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional (Boolean includeTaskCanceledExceptions) [0x00000] in <filename unknown>:0 at System.Threading.Tasks.Task`1[System.Int32].GetResultCore (Boolean waitCompletionNotification) [0x00000] in <filename unknown>:0 at System.Threading.Tasks.Task`1[System.Int32].get_Result () [0x00000] in <filename unknown>:0 at Recetas.Multithreading.R0407.GestionExcepcionesTarea.Main () [0x00000] in <filename unknown>:0 ---> (Inner Exception #0) System.Exception: ¡Boom! at Recetas.Multithreading.R0407.GestionExcepcionesTarea.Tarea (System.String nombre, Int32 duracion) [0x00000] in <filename unknown>:0 at Recetas.Multithreading.R0407.GestionExcepcionesTarea.<Main>m__0 () [0x00000] in <filename unknown>:0 at System.Threading.Tasks.Task`1[System.Int32].InnerInvoke () [0x00000] in <filename unknown>:0 at System.Threading.Tasks.Task.Execute () [0x00000] in <filename unknown>:0 <--- . ------------------------------------------------ La tarea `Tarea No. 2` se está ejecutando en el ID de Thread 5. ¿Thread en pool de threads?: True La tarea `Tarea No. 4` se está ejecutando en el ID de Thread 4. ¿Thread en pool de threads?: True Excepción de `tareaCompuesta`: System.AggregateException: One or more errors occurred. ---> System.Exception: ¡Boom! at Recetas.Multithreading.R0407.GestionExcepcionesTarea.Tarea (System.String nombre, Int32 duracion) [0x00000] in <filename unknown>:0 at Recetas.Multithreading.R0407.GestionExcepcionesTarea.<Main>m__1 () [0x00000] in <filename unknown>:0 at System.Threading.Tasks.Task`1[System.Int32].InnerInvoke () [0x00000] in <filename unknown>:0 at System.Threading.Tasks.Task.Execute () [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- ---> (Inner Exception #0) System.Exception: ¡Boom! at Recetas.Multithreading.R0407.GestionExcepcionesTarea.Tarea (System.String nombre, Int32 duracion) [0x00000] in <filename unknown>:0 at Recetas.Multithreading.R0407.GestionExcepcionesTarea.<Main>m__1 () [0x00000] in <filename unknown>:0 at System.Threading.Tasks.Task`1[System.Int32].InnerInvoke () [0x00000] in <filename unknown>:0 at System.Threading.Tasks.Task.Execute () [0x00000] in <filename unknown>:0 <--- ---> (Inner Exception #1) System.Exception: ¡Boom! at Recetas.Multithreading.R0407.GestionExcepcionesTarea.Tarea (System.String nombre, Int32 duracion) [0x00000] in <filename unknown>:0 at Recetas.Multithreading.R0407.GestionExcepcionesTarea.<Main>m__2 () [0x00000] in <filename unknown>:0 at System.Threading.Tasks.Task`1[System.Int32].InnerInvoke () [0x00000] in <filename unknown>:0 at System.Threading.Tasks.Task.Execute () [0x00000] in <filename unknown>:0 <---