Created
July 19, 2014 00:34
-
-
Save Fhernd/23cd08057e5030709a83 to your computer and use it in GitHub Desktop.
Demostración del uso de la propiedad HResult de Exception.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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