Académique Documents
Professionnel Documents
Culture Documents
Lenguaje de programación i
GESTIÓN DE
EXCEPCIONES
Los Temas Principales
Semana 01
Gestión de excepciones en Java
“Exception" significa “Condición Excepcional“.
try {
obtenerArchivoDeLaRed
leerArchivoYCargarTabla
} catch(NoPuedeObtenerArcihvoDeLaRed) {
mostrarMensajeDeErrorDeRed
}
Un bloque FINALLY encierra código que es siempre
ejecutado después del bloque TRY, sea que se produzca o
no una excepción. FINALLY se ejecuta, incluso, si existe
una sentencia RETURN dentro del bloque TRY:
try { try {
// hacer algo // hacer algo
} finally { } catch (SomeException ex) {
//hacer algo siempre // en caso de excepcion
} } finally {
// hacer algo siempre
}
ESCENARIOS
try { try {
// hacer algo // hacer algo
} }
System.out.println("fuera del System.out.println("fuera del
bloque try"); bloque try");
catch(Exception ex) {
// en caso de excepcion
}
La mayoría de lenguajes manejan el concepto de pila de
llamadas o pila de métodos.
C
B
PROPAGACIÓN DE
A
MAIN
EXCEPCIONES
excepciones.
try {
// colocamos algún código aquí
}
catch (ArraylndexOutOfBoundsException e) {
e.printStackTrace();
}
JERARQUIA DE
EXCEPCIONES
try {
// Algun codigo que pueda lanzar una boundary
exception
}
catch (IndexOutofBoundsException e) {
e.printStackTrace ();
}
EVITAR SENTENCIAS COMO LA QUE SE MUESTRA
try {
// some code
}catch (Exception e) {
e.printStackTrace();
}
JERARQUÍA DE EXCEPCIONES
class Plane {
static String s = "-";
public static void main(String[] args) {
Plane p = new Plane();
p.s1() ; ¿Cuál será el resultado de su ejecución?
System.out.println(s); a) -
} b) -c
void s1() { c) -c2
try { s2(); d) -2c
}catch (Exception e) { s += "c"; } e) -c22b
} f) -2c2b
void s2() throws Exception { g) -2c2bc
s3(); s += "2"; h) Compilation fails
s3(); s += "2b";
}
void s3() throws Exception {
throw new Exception();
}
}
EJERCICIOS
class Swill {
public static void main(String[] args) {
String s = "-";
switch(TimeZone.CST) {
case EST: s += "e"; ¿Cuál será el resultado de su ejecución?
case CST: s += "c"; a) -c
case MST: s += "m"; b) -X
default: s += "X"; c) -cm
case PST: s += "p"; d) -cmp
} e) -cmXp
System.out.println(s); f) Compilation fails.
} g) An exception is thrown at runtime.
}
enum TimeZone {EST, CST, MST, PST }
EJERCICIOS
a) ClassCastException
b) IllegalStateException
c) NumberFormatException
d) IllegalArgumentException
e) ExceptionInInitializerError
f) ArrayIndexOutOfBoundsException
Excepciones y errores comunes
en Java
ORIGEN DE LAS EXCEPCIONES Excepciones de la JVM. Son las excepciones y
errores lanzadas por la Java Virtual Machine.
void go() {
go();
}
en .
Saludos!