Académique Documents
Professionnel Documents
Culture Documents
1 / 33
Plan
1 Introduction
2 Capture d’
exception
3 Les exceptions personnalisées
6 Le bloc finally
2 / 33
Introduction
Java
3 / 33
Introduction
Java
4 / 33
Introduction
Java
exception :
exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
Ⓧ
c
System.out.print(x / y);
System.out.println("Fin de calcul");
}
}
5 / 33
Introduction
Java
exception :
exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
Ⓧ
c
System.out.print(x / y);
System.out.println("Fin de calcul");
}
}
5 / 33
Introduction
Java
exception :
exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
Ⓧ
c
System.out.print(x / y);
System.out.println("Fin de calcul");
}
}
Constatation
Le message Fin de calcul n’a pas étéaffiché
La division par zéro déclenche une exception Arithmetic exception
5 / 33
Capture d’ exception
Java
6 / 33
Capture d’
exception
Java
exception :
exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
Ⓧ
try {
}
System.out.println(x / y);
c
catch (Arithmetic exception e) { System.out.println("
exception : Division par z´ero ");
}
MOU HI
System.out.println("Fin de calcul");
}
}
7 / 33
Capture d’
exception
Java
exception :
exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
Ⓧ
try {
}
System.out.println(x / y);
c
catch (Arithmetic exception e) { System.out.println("
exception : Division par z´ero ");
}
System.out.println("Fin de calcul");
MOU HI
}
}
7 / 33
Capture d’
exception
Java
exception :
exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
Ⓧ
try {
}
System.out.println(x / y);
c
catch (Arithmetic exception e) { System.out.println("
exception : Division par z´ero ");
}
System.out.println("Fin de calcul");
MOU HI
}
}
Constatation
L’ exception a étécapturée
Le message Fin de calcul a été affiché
7 / 33
Capture d’
exception
Java
Et si je ne connais pas le type d’
exception
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
Ⓧ
c
try {
System.out.println(x / y);
}
catch ( exception e) {
System.out.println(" exception : Division par z´ero ");
}
MOU HI
System.out.println("Fin de calcul");
}
}
8 / 33
Capture d’
exception
Java
Et si je ne connais pas le type d’
exception
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
Ⓧ
c
try {
System.out.println(x / y);
}
catch ( exception e) {
System.out.println(" exception : Division par z´ero ");
}
System.out.println("Fin de calcul"); MOU HI
}
}
8 / 33
Capture d’
exception
Java
Et si je ne connais pas le type d’
exception
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
Ⓧ
c
try {
System.out.println(x / y);
}
catch ( exception e) {
System.out.println(" exception : Division par z´ero ");
}
System.out.println("Fin de calcul"); MOU HI
}
}
Constatation
La classe exception peut être utilisée
8 / 33
Capture d’
exception
Java
Utiliser des méthodes de la classe
exception
public class Test {
public static void main(String[] args) {
Ⓧ
c
int x = 5, y = 0;
try {
System.out.println(x / y);
}
catch ( exception e) {
MOU HI
System.out.println(" exception : " + e.getMessage());
}
System.out.println("Fin de calcul");
}
}
Ⓧ
c
9 / 33
Capture d’
exception
Java
Utiliser des méthodes de la classe
exception
public class Test {
public static void main(String[] args) {
Ⓧ
c
int x = 5, y = 0;
try {
System.out.println(x / y);
}
catch ( exception e) {
MOU HI
System.out.println(" exception : " + e.getMessage());
}
System.out.println("Fin de calcul");
}
}
Ⓧ
c
Le même message sera affiché
exception : / by zero
Fin de calcul
9 / 33
Capture d’
exception
Java
Utiliser des méthodes de la classe
exception
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
try {
System.out.println(x / y); Ⓧ
c
}
MOU HI
catch ( exception e) {
e.printStackTrace();
}
System.out.println("Fin de calcul");
Ⓧ
c
}
}
10 / 33
Capture d’
exception
Java
Utiliser des méthodes de la classe
exception
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
try {
System.out.println(x / y); Ⓧ
c
}
MOU HI
catch ( exception e) {
e.printStackTrace();
}
System.out.println("Fin de calcul");
Ⓧ
c
}
}
10 / 33
Les exceptions personnalisées
Java
NullPointerException
11 / 33
Les exceptions personnalisées
Java
NullPointer
exception
11 / 33
Les exceptions personnalisées
Java
Considérons la classe Adresse suivante
package org.eclipse.model;
Ⓧ
c
public class Adresse {
private String rue;
private String ville;
private String codePostal;
public Adresse(String rue, String codePostal,
MOU HI
String ville) {
Ⓧ
c
this.rue = rue;
this.ville = ville;
this.codePostal = codePostal;
}
// ensuite les getters/setters et autres m´ethodes
}
12 / 33
Les exceptions personnalisées
Java
Supposons que
codePostal doit contenir exactement 5 chiffres
13 / 33
Les exceptions personnalisées
Java
Supposons que
codePostal doit contenir exactement 5 chiffres
Démarche à faire
Créer notre propre exception (qui doit étendre la classe
exception)
Dans le constructeur de Adresse, on lance une exception
si codePostal ne contient pas 5 chiffres
13 / 33
Les exceptions personnalisées
Java
Ⓧ
c
public IncorrectCodePostalException(){
System.out.println("Le code postal doit
contenir exactement 5 chiffres");
}
}
14 / 33
Les exceptions personnalisées
Java
Modifions le constructeur de la classe Adresse
public class Adresse {
// apr`es les attributs
public Adresse(String rue, String ville, String
codePostal) throws IncorrectCodePostalException
{
if (codePostal.length() != 5) MOU HI
throw new IncorrectCodePostal
exception(); this.rue = rue;
this.ville = ville;
this.codePostal = codePostal;
}
}
// il faut faire pareil dans setCodePostal()
15 / 33
Les exceptions personnalisées
Java
Testons tout cela dans le main()
public static void main(String[] args) {
Adresse a = null;
try {
a = new Adresse ("rue de paradis", "Marseille",
"1300");
} MOU HI
catch(IncorrectCodePostalException e) {
e.printStackTrace();
}
}
16 / 33
Les exceptions personnalisées
Java
Testons tout cela dans le main()
public static void main(String[] args) {
Adresse a = null;
try {
a = new Adresse ("rue de paradis", "Marseille",
"1300");
} MOU HI
catch(IncorrectCodePostal exception icpe)
{ icpe.printStackTrace();
}
}
16 / 33
Les instructions multi-catch
Java
17 / 33
Les instructions multi-catch
Java
Ⓧ
c
System.out.print("Le nom de la rue doit ˆetre
en majuscule");
}
18 / 33
Les instructions multi-catch
Java
Modifions le constructeur de la classe Adresse
public class Adresse {
// apr`es les attributs
public Adresse(String rue, String ville, String
codePostal) throws IncorrectCodePostal Ⓧ
c
exception
, IncorrectStreetName exception
{ if (codePostal.length() != 5)
MOU HI
throw new IncorrectCodePostal
Ⓧ
c
exception();
if(!rue.equals(rue.toUpperCase()))
throw new IncorrectStreetName
exception(); this.rue = rue;
this.ville = ville;
this.codePostal = codePostal;
}
} 19 / 33
Les instructions multi-catch
Java
Ⓧ
c
try {
Adresse a = new Adresse ("paradis", "Marseille",
"1300");
}
catch(IncorrectCodePostal exception icp) MOU HI
{ icp.printStackTrace();
Ⓧ
c
}
catch(IncorrectStreetName exception isn)
{ isn.printStackTrace();
}
}
20 / 33
Les instructions multi-catch
Java
}
"1300");
MOU HI
catch(IncorrectCodePostal exception
| IncorrectStreetName exception e)
Ⓧ
c
{ e.printStackTrace();
}
}
21 / 33
Les exceptions paramétrées
Java
Question
Comment faire si on veut afficher les valeurs qui ont déclenché l’
exception dans le message ?
22 / 33
Les exceptions paramétrées
Java
} Ⓧ
c
doit contenir exactement 5 chiffres");
23 / 33
Les exceptions paramétrées
24 / 33
Les exceptions paramétrées
Java
Modifions le constructeur de la classe Adresse
public class Adresse {
// apr`es les attributs
Ⓧ
c
public Adresse(String rue, String ville, String
codePostal) throws IncorrectCodePostal
exception
, IncorrectStreetName exception
{ if (codePostal.length()!=5)
MOU HI
throw new IncorrectCodePostal
Ⓧ
cexception( codePostal);
if(!rue.equals(rue.toUpperCase()))
throw new IncorrectStreetName
exception(rue); this.rue = rue;
this.ville = ville;
this.codePostal = codePostal;
}
} 25 / 33
Les exceptions paramétrées
Java
Pour tester
public static void main(String[] args) {
try {
Adresse a = new Adresse ("paradis", "Marseille",
"1300");
}
catch(IncorrectCodePostal exception MOU HI
| IncorrectStreetName exception e)
{ e.printStackTrace();
}
}
26 / 33
Les exceptions paramétrées
Java
Pour tester
public static void main(String[] args) {
try {
Adresse a = new Adresse ("paradis", "Marseille",
"1300");
}
catch(IncorrectCodePostal exception MOU HI
| IncorrectStreetName exception e)
{ e.printStackTrace();
}
}
26 / 33
Les exceptions paramétrées
Java
Exercice
Créer une nouvelle classe d’ exception Adresse exception
pour fusionner et remplacer les deux exceptions
IncorrectCodePostal exception et
IncorrectStreetName exception
27 / 33
Les exceptions paramétrées
Java
Contenu de la classe Adresse exception
package org.eclipse.Exception;
public class AdresseException extends Exception {
MOU HI
System.out.print("Le nom de la rue ’" + rue + "’ doit ˆetre en
majuscule.");
}
Ⓧ
public AdresseException(int index, String value) {
c
if (index == 0)
System.out.println("Le code postal ’" + value + "’ doit contenir
exactement 5 chiffres.");
else
System.out.print("Le nom de la rue ’" + value + "’ doit ˆetre en
majuscule.");
}
}
28 / 33
Les exceptions paramétrées
Ⓧ
throw new Adresse exception(codePostal,
rue); setRue(rue);
setCodePostal(codePostal);
c
this.ville = ville;
}
MOU HI
public void setRue(String rue) throws Adresse exception {
if (!rue.equals(rue.toUpperCase()))
throw new Adresse exception(1, rue);
this.rue = rue;
Ⓧ
c
}
public void setCodePostal(String codePostal) throws Adresse exception
{
if (codePostal.length() != 5)
throw new Adresse exception(0,
codePostal); this.codePostal = codePostal;
}
// + les autres getters + setters + toString()
}
29 / 33
Les exceptions paramétrées
Java
Testons tout cela dans le main()
public static void main(String[] args) {
Adresse a = null;
try {
Ⓧ
c
a = new Adresse ("rue de paradis", "Marseille", "1300");
}
catch (Adresse exception e)
{ e.printStackTrace();
}
}
MOU HI
30 / 33
Les exceptions paramétrées
Java
Testons tout cela dans le main()
public static void main(String[] args) {
Adresse a = null;
try {
Ⓧ
c
a = new Adresse ("rue de paradis", "Marseille", "1300");
}
catch (Adresse exception e)
{ e.printStackTrace();
}
}
MOU HI
30 / 33
Le bloc finally
Java
31 / 33
Le bloc finally
Java
Exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
try {
System.out.println(x / y);
}
catch ( exception e) { MOU HI
System.out.println("Division par z´ero");
}
Ⓧ
c
finally{
System.out.println("Instruction ex´ecut´ee syst´e
matiquement");
}
}
}
32 / 33
Le bloc finally
Java
Remarque
Le bloc finally peut s’avérer intéressant si le catch contient un
return qui forcera l’arrêt de l’exécution du code. Malgré cela, ce bloc
(finally) sera exécuté.
33 / 33