Created
July 29, 2014 02:40
-
-
Save Fhernd/ed74b24db2fb6a07305c to your computer and use it in GitHub Desktop.
Demostración del uso de la excepción ArgumentNullException 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 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