Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 19, 2014 00:34
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/23cd08057e5030709a83 to your computer and use it in GitHub Desktop.
Save Fhernd/23cd08057e5030709a83 to your computer and use it in GitHub Desktop.
Demostración del uso de la propiedad HResult de Exception.
using System;
namespace Articulos.Excepciones.Parte4
{
// Crea una versión personalizada de la excepción
// de división entre cero (0):
public class DivisonPorCeroException : Exception
{
private const int DivisionPorCeroHResult = unchecked((int)0x81234567);
// Asigna un valor a la propiedad HResult heredada:
public DivisonPorCeroException( string mensaje, Exception excepcionAnidada)
: base (string.Format("(HRESULT:0x{1:X8}) {0}", mensaje, DivisionPorCeroHResult),
excepcionAnidada)
{
HResult = DivisionPorCeroHResult;
}
}
public sealed class UsoHResult
{
public static void Main()
{
Console.WriteLine ("\nEjemplo de uso de la propiedad `HResult`\n");
// Genera una excepción de forma intencionada:
try
{
try
{
int cero = 0;
int div = 1 / cero;
}
catch(Exception e)
{
throw new DivisonPorCeroException(
"Excepción por intento de división por cero. Genera una segunda excepción.",
e
);
}
}
catch( Exception e)
{
Console.WriteLine (e.ToString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment