Tipos de datos y excepciones en Java
Jim White
Java Developer
public static void main(String[] args) {
ArrayList<String> allStars = new ArrayList<String>();
allStars.add("Mays");
allStars.add("Aaron");
allStars.add("Ruth");
String last = allStars.get(3); // This will cause an exception
System.out.println(last);
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of
bounds for length 3
try {
// Sentencias a ejecutar y vigilar por excepciones
}
try {
// Sentencias a ejecutar y vigilar por excepciones
} catch (<Exception type> <variable name>) {
// Sentencias a ejecutar si ocurre una excepción
}
try {
ArrayList<String> allStars = new ArrayList<String>();
allStars.add("Mays");
allStars.add("Aaron");
allStars.add("Ruth");
String last = allStars.get(3); // Accessing an element that isn't there
System.out.println(last);
} catch (IndexOutOfBoundsException e) {
System.out.println("Oops - wrong index");
}
Oops - wrong index
catch para manejar distintos tipos de excepcionescatch tiene su propio tipo de excepción y variable de excepcióncatch en orden hasta encontrar coincidenciatry {
ArrayList<String> nums
= new ArrayList<String>();
nums.add("one");
String last = nums.get(0);
Integer.valueOf(last);
System.out.println(last);
} catch (IndexOutOfBoundsException eIndex) {
System.out.println("Oops - wrong index");
} catch (NumberFormatException eValueOf) {
System.out.println(
"Oops - String not a number");
}
Oops - String not a number
catch (Exception variable){ } como comodíntry {
// Código a probar
} catch (IndexOutOfBoundsException eIndex) {
System.out.println("Oops - wrong index");
} catch (NumberFormatException eValueOf) {
System.out.println("Oops - String not a number");
} catch (Exception e) { // Maneja todas salvo las dos anteriores
System.out.println("Pasó algo imprevisto");
}
finally siempre se ejecuta// Cuando ocurre una excepción
try {
Integer.valueOf("one");
} catch (NumberFormatException eValueOf) {
System.out.println("Oops");
} finally {
System.out.println("Haciendo limpieza");
}
Oops
Haciendo limpieza
// Cuando no se lanza ninguna excepción
try {
Integer.valueOf("1");
} catch (NumberFormatException eValueOf) {
System.out.println("Oops");
} finally {
System.out.println("Haciendo limpieza");
}
Haciendo limpieza
Exception pasado al bloque catch contiene mucha información.getMessage() para obtener la causa legible.getClass() para obtener la clase o tipo de la excepción .printStackTrace() para mostrar el stack trace (siguiente diapositiva)public class ExceptionExample {
public static void main(String[] args) {
try {
Integer.valueOf("one");
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
For input string: "one"
java.lang.NumberFormatException: For input string: "one"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:661)
at java.base/java.lang.Integer.valueOf(Integer.java:988)
at foo/exceptions.ExceptionExample.main(ExceptionExample.java:7)
Tipos de datos y excepciones en Java