Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 29, 2014 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/37259acfd3757cb1ef46 to your computer and use it in GitHub Desktop.
Save Fhernd/37259acfd3757cb1ef46 to your computer and use it in GitHub Desktop.
Demostración del uso de la excepción ArgumentOutOfRangeException en C#.
using System;
namespace Articulos.Cap04.Excepciones.Parte5
{
public class Invitado
{
private String Nombre;
private String Apellido;
private int Edad;
public Invitado(String nombre, String apellido, int edad)
{
Nombre = nombre;
Apellido = apellido;
// Valida que el invitado no sea menor de 30 años.
if (edad <= 30)
{
throw new ArgumentOutOfRangeException("edad", "Los invitados deben ser mayores de 30.");
}
else
{
Edad = edad;
}
}
// Muestra resumen del invitado:
public override String ToString()
{
return String.Format( "Nombre: {0} - Apellido: {1} - Edad: {2}",
Nombre,
Apellido,
Edad
);
}
}
public sealed class UsoArgumentOutOfRangeException
{
public static void Main()
{
try
{
// Menor de 30:
Invitado i = new Invitado("Daniela", "Ortiz", 20);
Console.WriteLine (i.ToString());
}
catch (ArgumentOutOfRangeException aoore)
{
Console.WriteLine ("\nMensaje de error: {0}",
aoore.Message
);
}
Console.WriteLine ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment