Created
July 29, 2014 03:04
-
-
Save Fhernd/37259acfd3757cb1ef46 to your computer and use it in GitHub Desktop.
Demostración del uso de la excepción ArgumentOutOfRangeException en C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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