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 ();
}
}
}
dXNpbmcgU3lzdGVtOwoKbmFtZXNwYWNlIEFydGljdWxvcy5DYXAwNC5FeGNlcGNpb25lcy5QYXJ0ZTUKewogICAgcHVibGljIGNsYXNzIEludml0YWRvCiAgICB7CiAgICAgICAgcHJpdmF0ZSBTdHJpbmcgTm9tYnJlOwogICAgICAgIHByaXZhdGUgU3RyaW5nIEFwZWxsaWRvOwogICAgICAgIHByaXZhdGUgaW50IEVkYWQ7CiAgICAgICAgCiAgICAgICAgcHVibGljIEludml0YWRvKFN0cmluZyBub21icmUsIFN0cmluZyBhcGVsbGlkbywgaW50IGVkYWQpCiAgICAgICAgewogICAgICAgICAgICBOb21icmUgPSBub21icmU7CiAgICAgICAgICAgIEFwZWxsaWRvID0gYXBlbGxpZG87CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBWYWxpZGEgcXVlIGVsIGludml0YWRvIG5vIHNlYSBtZW5vciBkZSAzMCBhw7Fvcy4KICAgICAgICAgICAgaWYgKGVkYWQgPD0gMzApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHRocm93IG5ldyBBcmd1bWVudE91dE9mUmFuZ2VFeGNlcHRpb24oImVkYWQiLCAiTG9zIGludml0YWRvcyBkZWJlbiBzZXIgbWF5b3JlcyBkZSAzMC4iKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIEVkYWQgPSBlZGFkOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIAogICAgICAgIC8vIE11ZXN0cmEgcmVzdW1lbiBkZWwgaW52aXRhZG86CiAgICAgICAgcHVibGljIG92ZXJyaWRlIFN0cmluZyBUb1N0cmluZygpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gU3RyaW5nLkZvcm1hdCggIk5vbWJyZTogezB9IC0gQXBlbGxpZG86IHsxfSAtIEVkYWQ6IHsyfSIsIAogICAgICAgICAgICAgICAgTm9tYnJlLCAKICAgICAgICAgICAgICAgIEFwZWxsaWRvLCAKICAgICAgICAgICAgICAgIEVkYWQKICAgICAgICAgICAgKTsKICAgICAgICB9CiAgICB9CiAgICAKICAgIHB1YmxpYyBzZWFsZWQgY2xhc3MgVXNvQXJndW1lbnRPdXRPZlJhbmdlRXhjZXB0aW9uCiAgICB7CiAgICAgICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQogICAgICAgIHsKICAgICAgICAgICAgdHJ5CiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIC8vIE1lbm9yIGRlIDMwOgogICAgICAgICAgICAgICAgSW52aXRhZG8gaSA9IG5ldyBJbnZpdGFkbygiRGFuaWVsYSIsICJPcnRpeiIsIDIwKTsKICAgICAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lIChpLlRvU3RyaW5nKCkpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGNhdGNoIChBcmd1bWVudE91dE9mUmFuZ2VFeGNlcHRpb24gYW9vcmUpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgiXG5NZW5zYWplIGRlIGVycm9yOiB7MH0iLCAKICAgICAgICAgICAgICAgICAgICBhb29yZS5NZXNzYWdlCiAgICAgICAgICAgICAgICApOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSAoKTsKICAgICAgICB9CiAgICB9Cn0=