Tipi di dati ed eccezioni in 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 {
// Istruzioni da eseguire e monitorare per eccezioni
}
try {
// Istruzioni da eseguire e monitorare per eccezioni
} catch (<Exception type> <variable name>) {
// Istruzioni da eseguire se si verifica un’eccezione
}
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("Ops - indice sbagliato");
}
Ops - indice sbagliato
catch per gestire tipi diversi di eccezionicatch ha il suo tipo di eccezione e la sua variabilecatch in ordine finché trova una corrispondenzatry {
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("Ops - indice sbagliato");
} catch (NumberFormatException eValueOf) {
System.out.println(
"Ops - String non è un numero");
}
Ops - String non è un numero
catch (Exception variable){ } come catch genericotry {
// Codice da provare
} catch (IndexOutOfBoundsException eIndex) {
System.out.println("Ops - indice sbagliato");
} catch (NumberFormatException eValueOf) {
System.out.println("Ops - String non è un numero");
} catch (Exception e) { // Gestisce tutte le eccezioni tranne le due sopra
System.out.println("È successo qualcosa di imprevisto");
}
finally viene sempre eseguito// Quando avviene un’eccezione
try {
Integer.valueOf("one");
} catch (NumberFormatException eValueOf) {
System.out.println("Ops");
} finally {
System.out.println("Pulizia in corso");
}
Ops
Pulizia in corso
// Quando non viene lanciata alcuna eccezione
try {
Integer.valueOf("1");
} catch (NumberFormatException eValueOf) {
System.out.println("Ops");
} finally {
System.out.println("Pulizia in corso");
}
Pulizia in corso
Exception passato nel catch è ricco di info.getMessage() per la causa leggibile.getClass() per ottenere la classe o il tipo dell’eccezione .printStackTrace() per mostrare lo stack trace (slide successiva)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)
Tipi di dati ed eccezioni in Java