Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2.  
  3. namespace Articulos.Cap04.Excepciones.Parte5
  4. {
  5.     public sealed class UsoArgumentNullException
  6.     {
  7.         // Método que muestra un mensaje en la salida estándar:
  8.         private static void MostrarMensaje(String mensaje)
  9.         {
  10.             // Se lanza la excepción cuando el argumento `mensaje`
  11.             // es asociado con una referencia null:
  12.             if (mensaje == null)
  13.             {
  14.                 throw new ArgumentNullException ("La cadena de texto a mostrar debe ser válida.", "mensaje");
  15.             }
  16.            
  17.             Console.WriteLine (mensaje);
  18.         }
  19.    
  20.         public static void Main()
  21.         {
  22.             // "Bienvenidos a xCSw":
  23.             MostrarMensaje("Bienvenidos a xCSw");
  24.            
  25.             try
  26.             {
  27.                 // Cuando pasamos
  28.                 String cadena = null;
  29.                 MostrarMensaje(cadena);
  30.             }
  31.             catch (ArgumentNullException ae)
  32.             {
  33.                 Console.WriteLine ("\nMensaje de error: `{0}`", ae.Message);
  34.             }
  35.             Console.WriteLine ();
  36.         }
  37.     }
  38. }