Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 18, 2014 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/d9d7a8ec4be88ab0f3ee to your computer and use it in GitHub Desktop.
Save Fhernd/d9d7a8ec4be88ab0f3ee to your computer and use it in GitHub Desktop.
Demostración del uso de la propiedad System.Exception.Data.
using System;
using System.Collections;
namespace Articulos.Excepciones.Parte4
{
public sealed class UsoData
{
public static void Main()
{
UsarPropiedadData();
}
public static void UsarPropiedadData()
{
try
{
GenerarExcepcion();
}
catch (Exception e)
{
Console.WriteLine ("\nSe ha generado una excepción.");
Console.WriteLine ("\t{0}", e.Message);
// Valida que la propiedad Data contiene elementos:
if (e.Data.Count > 0)
{
Console.WriteLine ("\nDetalles adicionales de la excepción:");
foreach (DictionaryEntry de in e.Data)
{
Console.WriteLine ("\tLlave: {0,-20}Valor: {1}",
de.Key.ToString(),
de.Value.ToString()
);
}
Console.WriteLine();
}
}
}
public static void GenerarExcepcion()
{
Exception e = new Exception();
DateTime fechaHora = DateTime.Now;
string mensaje = "Excepción generada en el método GenerarExcepcion.";
int codigo = 852963741;
// Agrega pares llave/valor a la propiedad Data:
e.Data["ID"] = codigo;
e.Data["FechaHora"] = fechaHora;
e.Data["Mensaje"] = mensaje;
throw e;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment