Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 19, 2014 01:39
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/cd7a578183aace9c48ce to your computer and use it in GitHub Desktop.
Save Fhernd/cd7a578183aace9c48ce to your computer and use it in GitHub Desktop.
Demostración del uso de las propiedades TargetSite, HelpLink, Source, y StackTrace de la clase Exception.
using System;
namespace Articulos.Excepciones.Parte4
{
// Clase personalidad para registros de desbordamiento:
public class TablaRegistroException : Exception
{
private const string mensajeDesbordamiento =
"Se ha generado un desbordamiento de memoria.";
public TablaRegistroException(string mensaje, Exception inner) :
base (String.Format ("{0} - {1}",
mensajeDesbordamiento, mensaje) , inner)
{
this.HelpLink = "http://ortizol.blogspot.com";
this.Source = "xCSw_Exceptiones";
}
}
public class TablaRegistro
{
protected int elementoActual;
protected string[] registros;
public TablaRegistro (int numeroElementos)
{
registros = new string [numeroElementos];
elementoActual = 0;
}
// Agrega un nuevo registro a la tabla:
public int AgregarRegistro(string registro)
{
try
{
registros[elementoActual] = registro;
return ++elementoActual;
}
catch (Exception e)
{
throw new TablaRegistroException (
String.Format("Registro `{0}` no fue agregado.",
registro), e
);
}
}
}
public sealed class UsoTargetSite
{
public static void Main()
{
// Creación de la tabla de registros:
TablaRegistro tr = new TablaRegistro(4);
Console.WriteLine (String.Format ("\nDemostración del uso de las propiedades: \n\t`{0}`,\n\t`{1}`\n\t`{2}`\n\t`{3}`",
"TargetSite", "HelpLink", "Source", "StackTrace")
);
try
{
for (int i = 1; ; ++i)
{
tr.AgregarRegistro(
String.Format ("Número de registro: {0}", i.ToString())
);
}
}
catch (Exception ex)
{
Console.WriteLine ("\nDatos de la excepción generada:");
Console.WriteLine ("\t`Mensaje`: {0}", ex.Message);
Console.WriteLine ("\t`TargetSite`: {0}", ex.TargetSite);
Console.WriteLine ("\t`HelpLink`: {0}", ex.HelpLink);
Console.WriteLine ("\t`Source`: {0}", ex.Source);
Console.WriteLine ("\t`StackTrace`: {0}", ex.StackTrace);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment