Types de données et exceptions 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 {
// Instructions à exécuter et surveiller pour des exceptions
}
try {
// Instructions à exécuter et surveiller pour des exceptions
} catch (<Exception type> <variable name>) {
// Instructions à exécuter si une exception s'est produite
}
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("Oups - mauvais indice");
}
Oups - mauvais indice
catch pour gérer différents types d'exceptionscatch a son propre type d'exception et sa variablecatch dans l'ordre jusqu'à trouver une correspondancetry {
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("Oups - mauvais indice");
} catch (NumberFormatException eValueOf) {
System.out.println(
"Oups - la chaîne n'est pas un nombre");
}
Oups - la chaîne n'est pas un nombre
catch (Exception variable){ } comme filet de sécuritétry {
// Code à essayer
} catch (IndexOutOfBoundsException eIndex) {
System.out.println("Oups - mauvais indice");
} catch (NumberFormatException eValueOf) {
System.out.println("Oups - la chaîne n'est pas un nombre");
} catch (Exception e) { // Gère toutes les exceptions sauf les deux ci-dessus
System.out.println("Quelque chose d'imprévu s'est produit");
}
finally s'exécute toujours// Quand une exception survient
try {
Integer.valueOf("one");
} catch (NumberFormatException eValueOf) {
System.out.println("Oups");
} finally {
System.out.println("Nettoyage en cours");
}
Oups
Nettoyage en cours
// Quand aucune exception n'est lancée
try {
Integer.valueOf("1");
} catch (NumberFormatException eValueOf) {
System.out.println("Oups");
} finally {
System.out.println("Nettoyage en cours");
}
Nettoyage en cours
Exception passé au bloc catch contient beaucoup d'information.getMessage() pour le message lisible.getClass() pour le nom de la classe ou du type d'Exception.printStackTrace() pour afficher la pile d'appels (diapo suivante)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)
Types de données et exceptions en Java