Advertisement
Fhernd

UsoHResult.cs

Jul 18th, 2014
2,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.48 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Articulos.Excepciones.Parte4
  4. {
  5.     // Crea una versión personalizada de la excepción
  6.     // de división entre cero (0):
  7.     public class DivisonPorCeroException : Exception
  8.     {
  9.         private const int DivisionPorCeroHResult = unchecked((int)0x81234567);
  10.        
  11.         // Asigna un valor a la propiedad HResult heredada:
  12.         public DivisonPorCeroException( string mensaje, Exception excepcionAnidada)
  13.             : base (string.Format("(HRESULT:0x{1:X8}) {0}", mensaje, DivisionPorCeroHResult),
  14.                     excepcionAnidada)
  15.         {
  16.             HResult = DivisionPorCeroHResult;
  17.         }
  18.     }
  19.    
  20.     public sealed class UsoHResult
  21.     {
  22.         public static void Main()
  23.         {
  24.             Console.WriteLine ("\nEjemplo de uso de la propiedad `HResult`\n");
  25.            
  26.             // Genera una excepción de forma intencionada:
  27.             try
  28.             {
  29.                 try
  30.                 {
  31.                     int cero = 0;
  32.                     int div = 1 / cero;
  33.                 }
  34.                 catch(Exception e)
  35.                 {
  36.                     throw new DivisonPorCeroException(
  37.                         "Excepción por intento de división por cero. Genera una segunda excepción.",
  38.                         e
  39.                     );
  40.                 }
  41.             }
  42.             catch( Exception e)
  43.             {
  44.                 Console.WriteLine (e.ToString());
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement