Está en la página 1de 6

Excepciones:

-DivideByZeroException:
Que se genera en la división de enteros cuando el
divisor es 0. Por ejemplo, intentar dividir 10 entre 0
genera una excepción DivideByZeroException.

-NotFiniteNumberException:
Que se genera cuando se realiza una operación o
devuelve Double.NaN , Double.NegativeInfinity ,
Double.PositiveInfinity , Single.NaN ,
Single.NegativeInfinity , Single.PositiveInfinity y el
lenguaje de programación utilizado no admite esos
valores.

-OverflowException:
que se genera cuando el resultado de una
operación está fuera de los límites del tipo de datos
de destino. Es decir, es menor que MinValuela
propiedad de un número o mayor que su
MaxValuepropiedad. Por ejemplo, intentar asignar
200 + 200 a un valor de Byte genera una excepción
OverflowException , ya que 400 es mayor que 256,
el límite superior del tipo de datos Byte .

1
-NullReferenceException:
Excepción que se produce cuando se intenta
desreferenciar un objeto null. Se produce una
NullReferenceException excepción cuando se
intenta acceder a un miembro en un tipo cuyo
valor es null. Normalmente, una
NullReferenceException excepción refleja el
error del desarrollador

-OutOfMemoryException:
Excepción que se produce cuando no hay
suficiente memoria para continuar con la
ejecución de un programa.
OutOfMemoryException usa HRESULT
COR_E_OUTOFMEMORY, que tiene el valor
0x8007000E.

-StackOverflowException:
Excepción que se produce cuando la pila de
ejecución supera el tamaño de la pila. Esta
clase no puede heredarse.
StackOverflowException se produce para
errores de desbordamiento de pila de
ejecución, normalmente en el caso de una
recursividad muy profunda o sin enlazar. Por
lo tanto, asegúrese de que el código no tiene
un bucle infinito ni una recursividad infinita.

2
-InvalidCastException:
Excepción que se produce cuando la
conversión no es válida o es explícita.
.NET Framework admite la conversión
automática de tipos derivados a sus tipos base
y de vuelta al tipo derivado, así como de tipos
que presentan interfaces a objetos de interfaz
y de vuelta. También incluye una variedad de
mecanismos que admiten conversiones
personalizadas.

3
Ejemplo 1:
InvalidCastException
using System;

public class Example


{
public static void Main()
{
bool flag = true;
try {
IConvertible conv = flag;
Char ch = conv.ToChar(null);
Console.WriteLine("Conversion succeeded.");
}
catch (InvalidCastException) {
Console.WriteLine("Cannot convert a Boolean to a Char.");
}

try {
Char ch = Convert.ToChar(flag);
Console.WriteLine("Conversion succeeded.");
}
catch (InvalidCastException) {
Console.WriteLine("Cannot convert a Boolean to a Char.");
}
}
}
// The example displays the following output:
// Cannot convert a Boolean to a Char.
// Cannot convert a Boolean to a Char.

4
Ejemplo 2:
StackOverflowException
using System;

public class Example


{
private const int MAX_RECURSIVE_CALLS = 1000;
static int ctr = 0;

public static void Main()


{
Example ex = new Example();
ex.Execute();
Console.WriteLine("\nThe call counter: {0}", ctr);
}

private void Execute()


{
ctr++;
if (ctr % 50 == 0)
Console.WriteLine("Call number {0} to the Execute method", ctr);

if (ctr <= MAX_RECURSIVE_CALLS)


Execute();

ctr--;
}
}
// The example displays the following output:
// Call number 50 to the Execute method
// Call number 100 to the Execute method
// Call number 150 to the Execute method
// Call number 200 to the Execute method
// Call number 250 to the Execute method
// Call number 300 to the Execute method
// Call number 350 to the Execute method
// Call number 400 to the Execute method
// Call number 450 to the Execute method
// Call number 500 to the Execute method
// Call number 550 to the Execute method
// Call number 600 to the Execute method
// Call number 650 to the Execute method
// Call number 700 to the Execute method
// Call number 750 to the Execute method
// Call number 800 to the Execute method
// Call number 850 to the Execute method
// Call number 900 to the Execute method
// Call number 950 to the Execute method
// Call number 1000 to the Execute method
//
// The call counter: 0

5
Ejemplo 3:
OutOfMemoryException
using System;

public class Example


{
public static void Main()
{
try {
// Outer block to handle any unexpected exceptions.
try {
string s = "This";
s = s.Insert(2, "is ");

// Throw an OutOfMemoryException exception.


throw new OutOfMemoryException();
}
catch (ArgumentException) {
Console.WriteLine("ArgumentException in String.Insert");
}

// Execute program logic.


}
catch (OutOfMemoryException e) {
Console.WriteLine("Terminating application unexpectedly...");
Environment.FailFast(String.Format("Out of Memory: {0}",
e.Message));
}
}
}
// The example displays the following output:
// Terminating application unexpectedly...

También podría gustarte