Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 29, 2014 04:08
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/8e91f16dc3fe85953132 to your computer and use it in GitHub Desktop.
Save Fhernd/8e91f16dc3fe85953132 to your computer and use it in GitHub Desktop.
Demostración del uso de la excepción InvalidCastException en C#.
using System;
namespace Articulos.Cap04.Excepciones.Parte5
{
public class A {}
public class B : A {}
public sealed class UsoInvalidCastException
{
public static void Main()
{
try
{
A a = new A();
// La conversión de superclase a subclase
// en una jerarquía de herencia no está
// permitada.
// El siguiente intento de conversión generará
// la excepción InvalidCastException:
B b = (B) a;
}
catch (InvalidCastException ice)
{
Console.WriteLine ("Mensaje de error: `{0}`", ice.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment