Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 29, 2014 02:40
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/ed74b24db2fb6a07305c to your computer and use it in GitHub Desktop.
Save Fhernd/ed74b24db2fb6a07305c to your computer and use it in GitHub Desktop.
Demostración del uso de la excepción ArgumentNullException en C#.
using System;
namespace Articulos.Cap04.Excepciones.Parte5
{
public sealed class UsoArgumentNullException
{
// Método que muestra un mensaje en la salida estándar:
private static void MostrarMensaje(String mensaje)
{
// Se lanza la excepción cuando el argumento `mensaje`
// es asociado con una referencia null:
if (mensaje == null)
{
throw new ArgumentNullException ("La cadena de texto a mostrar debe ser válida.", "mensaje");
}
Console.WriteLine (mensaje);
}
public static void Main()
{
// "Bienvenidos a xCSw":
MostrarMensaje("Bienvenidos a xCSw");
try
{
// Cuando pasamos
String cadena = null;
MostrarMensaje(cadena);
}
catch (ArgumentNullException ae)
{
Console.WriteLine ("\nMensaje de error: `{0}`", ae.Message);
}
Console.WriteLine ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment