Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Collections;
  3.  
  4. namespace Articulos.Excepciones.Parte4
  5. {
  6.     public sealed class UsoData
  7.     {
  8.         public static void Main()
  9.         {
  10.             UsarPropiedadData();
  11.         }
  12.        
  13.         public static void UsarPropiedadData()
  14.         {
  15.             try
  16.             {
  17.                 GenerarExcepcion();
  18.             }
  19.             catch (Exception e)
  20.             {
  21.                 Console.WriteLine ("\nSe ha generado una excepción.");
  22.                 Console.WriteLine ("\t{0}", e.Message);
  23.                
  24.                 // Valida que la propiedad Data contiene elementos:
  25.                 if (e.Data.Count > 0)
  26.                 {
  27.                     Console.WriteLine ("\nDetalles adicionales de la excepción:");
  28.                     foreach (DictionaryEntry de in e.Data)
  29.                     {
  30.                         Console.WriteLine ("\tLlave: {0,-20}Valor: {1}",
  31.                             de.Key.ToString(),
  32.                             de.Value.ToString()
  33.                         );
  34.                     }
  35.                     Console.WriteLine();
  36.                 }
  37.             }
  38.         }
  39.        
  40.         public static void GenerarExcepcion()
  41.         {
  42.             Exception e = new Exception();
  43.            
  44.             DateTime fechaHora = DateTime.Now;
  45.             string mensaje = "Excepción generada en el método GenerarExcepcion.";
  46.             int codigo = 852963741;
  47.            
  48.             // Agrega pares llave/valor a la propiedad Data:
  49.             e.Data["ID"] = codigo;
  50.             e.Data["FechaHora"] = fechaHora;
  51.             e.Data["Mensaje"] = mensaje;
  52.            
  53.             throw e;
  54.         }
  55.     }
  56. }