Created
July 29, 2014 04:08
-
-
Save Fhernd/8e91f16dc3fe85953132 to your computer and use it in GitHub Desktop.
Demostración del uso de la excepción InvalidCastException 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 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