Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2.  
  3. namespace Articulos.Cap04.Excepciones.Parte5
  4. {
  5.     public class Invitado
  6.     {
  7.         private String Nombre;
  8.         private String Apellido;
  9.         private int Edad;
  10.        
  11.         public Invitado(String nombre, String apellido, int edad)
  12.         {
  13.             Nombre = nombre;
  14.             Apellido = apellido;
  15.            
  16.             // Valida que el invitado no sea menor de 30 aƱos.
  17.             if (edad <= 30)
  18.             {
  19.                 throw new ArgumentOutOfRangeException("edad", "Los invitados deben ser mayores de 30.");
  20.             }
  21.             else
  22.             {
  23.                 Edad = edad;
  24.             }
  25.         }
  26.        
  27.         // Muestra resumen del invitado:
  28.         public override String ToString()
  29.         {
  30.             return String.Format( "Nombre: {0} - Apellido: {1} - Edad: {2}",
  31.                 Nombre,
  32.                 Apellido,
  33.                 Edad
  34.             );
  35.         }
  36.     }
  37.    
  38.     public sealed class UsoArgumentOutOfRangeException
  39.     {
  40.         public static void Main()
  41.         {
  42.             try
  43.             {
  44.                 // Menor de 30:
  45.                 Invitado i = new Invitado("Daniela", "Ortiz", 20);
  46.                 Console.WriteLine (i.ToString());
  47.             }
  48.             catch (ArgumentOutOfRangeException aoore)
  49.             {
  50.                 Console.WriteLine ("\nMensaje de error: {0}",
  51.                     aoore.Message
  52.                 );
  53.             }
  54.            
  55.             Console.WriteLine ();
  56.         }
  57.     }
  58. }