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 ( "\n Ejemplo de uso de la propiedad `HResult`\n " ) ;
// Genera una excepción de forma intencionada:
try
{
try
{
int cero = 0 ;
}
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 ( ) ) ;
}
}
}
}
dXNpbmcgU3lzdGVtOwoKbmFtZXNwYWNlIEFydGljdWxvcy5FeGNlcGNpb25lcy5QYXJ0ZTQKewogICAgLy8gQ3JlYSB1bmEgdmVyc2nDs24gcGVyc29uYWxpemFkYSBkZSBsYSBleGNlcGNpw7NuIAogICAgLy8gZGUgZGl2aXNpw7NuIGVudHJlIGNlcm8gKDApOgogICAgcHVibGljIGNsYXNzIERpdmlzb25Qb3JDZXJvRXhjZXB0aW9uIDogRXhjZXB0aW9uCiAgICB7CiAgICAgICAgcHJpdmF0ZSBjb25zdCBpbnQgRGl2aXNpb25Qb3JDZXJvSFJlc3VsdCA9IHVuY2hlY2tlZCgoaW50KTB4ODEyMzQ1NjcpOwogICAgICAgIAogICAgICAgIC8vIEFzaWduYSB1biB2YWxvciBhIGxhIHByb3BpZWRhZCBIUmVzdWx0IGhlcmVkYWRhOgogICAgICAgIHB1YmxpYyBEaXZpc29uUG9yQ2Vyb0V4Y2VwdGlvbiggc3RyaW5nIG1lbnNhamUsIEV4Y2VwdGlvbiBleGNlcGNpb25BbmlkYWRhKQogICAgICAgICAgICA6IGJhc2UgKHN0cmluZy5Gb3JtYXQoIihIUkVTVUxUOjB4ezE6WDh9KSB7MH0iLCBtZW5zYWplLCBEaXZpc2lvblBvckNlcm9IUmVzdWx0KSwgCiAgICAgICAgICAgICAgICAgICAgZXhjZXBjaW9uQW5pZGFkYSkKICAgICAgICB7CiAgICAgICAgICAgIEhSZXN1bHQgPSBEaXZpc2lvblBvckNlcm9IUmVzdWx0OwogICAgICAgIH0KICAgIH0KICAgIAogICAgcHVibGljIHNlYWxlZCBjbGFzcyBVc29IUmVzdWx0CiAgICB7CiAgICAgICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQogICAgICAgIHsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJcbkVqZW1wbG8gZGUgdXNvIGRlIGxhIHByb3BpZWRhZCBgSFJlc3VsdGBcbiIpOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gR2VuZXJhIHVuYSBleGNlcGNpw7NuIGRlIGZvcm1hIGludGVuY2lvbmFkYToKICAgICAgICAgICAgdHJ5CiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHRyeQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGludCBjZXJvID0gMDsKICAgICAgICAgICAgICAgICAgICBpbnQgZGl2ID0gMSAvIGNlcm87CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBjYXRjaChFeGNlcHRpb24gZSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICB0aHJvdyBuZXcgRGl2aXNvblBvckNlcm9FeGNlcHRpb24oCiAgICAgICAgICAgICAgICAgICAgICAgICJFeGNlcGNpw7NuIHBvciBpbnRlbnRvIGRlIGRpdmlzacOzbiBwb3IgY2Vyby4gR2VuZXJhIHVuYSBzZWd1bmRhIGV4Y2VwY2nDs24uIiwgCiAgICAgICAgICAgICAgICAgICAgICAgIGUKICAgICAgICAgICAgICAgICAgICApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGNhdGNoKCBFeGNlcHRpb24gZSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKGUuVG9TdHJpbmcoKSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0=