Cours Java Exception
Cours Java Exception
Achref El Mouelhi
1 Introduction
2 Capture d’exception
6 Le bloc finally
Java
Java
Java
Exception : exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
System.out.print(x / y);
I c
}
System.out.println("Fin de calcul");
ELH
U
MO
}
Java
Exception : exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
System.out.print(x / y);
I c
}
System.out.println("Fin de calcul");
ELH
U
MO
}
Java
Exception : exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
System.out.print(x / y);
I c
}
System.out.println("Fin de calcul");
ELH
U
MO
}
Constatation
Le message Fin de calcul n’a pas été affiché
La division par zéro déclenche une exception ArithmeticException
Java
Java
Exception : exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
try {
System.out.println(x / y);
I c
}
catch (ArithmeticException e) {
ELH
U
MO
System.out.println("Exception : Division par zéro ");
}
E L
System.out.println("Fin de calcul");
f
hre
}
}
c
c A
Java
Exception : exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
try {
System.out.println(x / y);
I c
}
catch (ArithmeticException e) {
ELH
U
MO
System.out.println("Exception : Division par zéro ");
}
E
System.out.println("Fin de calcul");
f L
hre
}
}
c
c A
Le message affiché à l’exécution
Java
Exception : exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
try {
System.out.println(x / y);
I c
}
catch (ArithmeticException e) {
ELH
U
MO
System.out.println("Exception : Division par zéro ");
}
E
System.out.println("Fin de calcul");
f L
hre
}
}
c
c A
Le message affiché à l’exécution
Constatation
L’exception a été capturée
Le message Fin de calcul a été affiché
H & H: Research and Training 7 / 33
Capture d’exception
Java
Et si je ne connais pas le type d’exception
f E L
System.out.println("Fin de calcul");
hre
}
}
c
c A
Java
Et si je ne connais pas le type d’exception
f E
System.out.println("Fin de calcul"); L
hre
}
}
c
c A
Le même message sera affiché
Java
Et si je ne connais pas le type d’exception
f E
System.out.println("Fin de calcul"); L
hre
}
}
c
c A
Le même message sera affiché
Constatation
La classe Exception peut être utilisée
Java
Utiliser des méthodes de la classe Exception
}
}
Java
Utiliser des méthodes de la classe Exception
}
}
Le même message sera affiché
Exception : / by zero
Fin de calcul
Java
Utiliser des méthodes de la classe Exception
c
System.out.println("Fin de calcul");
}
}
c A
Java
Utiliser des méthodes de la classe Exception
c
System.out.println("Fin de calcul");
}
}
c A
Le message affiché est :
java.lang.ArithmeticException : / by zero
at test.FirstClass.main( FirstClass.java :49 )
Fin de calcul
Java
Java
Java
Considérons la classe Adresse suivante
package org.eclipse.model;
Java
Supposons que
codePostal doit contenir exactement 5 chiffres
I c
ELH
U
L MO
f E
c hre
c A
Java
Supposons que
codePostal doit contenir exactement 5 chiffres
I c
ELH
U
L MO
Démarche à faire
f E
c hre
c A
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
Java
I c
public class IncorrectCodePostalException extends
Exception {
EL H
U
// le constructeur de L MOnouvelle exception
fE
cette
r e
public IncorrectCodePostalException(){
c h
c A
System.out.println("Le
exactement
code postal doit contenir
5 chiffres");
}
}
Java
Java
Testons tout cela dans le main()
public static void main(String[] args) {
Adresse a = null;
try {
I c
a = new Adresse ("rue de paradis", "Marseille",
ELH
U
MO
"1300");
}
f E L
hre
catch(IncorrectCodePostalException icpe) {
c
c A
icpe.printStackTrace();
}
}
Java
Testons tout cela dans le main()
public static void main(String[] args) {
Adresse a = null;
try {
I c
a = new Adresse ("rue de paradis", "Marseille",
ELH
U
MO
"1300");
}
f E L
hre
catch(IncorrectCodePostalException icpe) {
c
c A
icpe.printStackTrace();
}
}
Le message affiché est :
Java
I c
On peut rajouter une deuxième condition
E LH
U
MO 5 chiffres
codePostal doit contenir exactement
L
r e fE
rue doit être une chaı̂ne
h
en majuscule
A c
c
Java
I c
public class IncorrectStreetNameException extends
Exception {
EL H
U
L MO
re f
System.out.print("Le
E
public IncorrectStreetNameException()
nom de la rue
{
doit être en
c h
c A
majuscule");
}
}
Java
Modifions le constructeur de la classe Adresse
public class Adresse {
// après les attributs
public Adresse(String rue, String ville, String
I c
codePostal) throws IncorrectCodePostalException
ELH
, IncorrectStreetNameException {
U
if (codePostal.length() != 5)
L MO
f E
throw new IncorrectCodePostalException();
c hre
if(!rue.equals(rue.toUpperCase()))
c A
throw new IncorrectStreetNameException();
this.rue = rue;
this.ville = ville;
this.codePostal = codePostal;
}
}
Java
Java
Java
I
c
Question
ELH
Comment faire si on veut afficher lesM
U
O qui ont déclenché
valeurs
l’exception dans le message ?E L
c h ref
c A
Java
}
c A
doit contenir exactement 5 chiffres");
f E L
public IncorrectStreetNameException(String rue) {
hre
System.out.print("Le nom de la rue ’" + rue + "’
c
c A
doit être en majuscule");
}
}
Java
Modifions le constructeur de la classe Adresse
public class Adresse {
// après les attributs
public Adresse(String rue, String ville, String
I c
codePostal) throws IncorrectCodePostalException
ELH
, IncorrectStreetNameException {
U
if (codePostal.length()!=5)
L MO
f E
throw new IncorrectCodePostalException(
codePostal);
c hre
c A
if(!rue.equals(rue.toUpperCase()))
throw new IncorrectStreetNameException(rue);
this.rue = rue;
this.ville = ville;
this.codePostal = codePostal;
}
}
H & H: Research and Training 25 / 33
Les exceptions paramétrées
Java
Pour tester
public static void main(String[] args) {
try {
Adresse a = new Adresse ("paradis", "Marseille",
I c
"1300");
ELH
U
MO
}
catch(IncorrectCodePostalException |
f E L
hre
IncorrectStreetNameException e) {
c
c A
e.printStackTrace();
}
}
Java
Pour tester
public static void main(String[] args) {
try {
Adresse a = new Adresse ("paradis", "Marseille",
I c
"1300");
ELH
U
MO
}
catch(IncorrectCodePostalException |
f E L
hre
IncorrectStreetNameException e) {
c
c A
e.printStackTrace();
}
}
Le message affiché est :
Java
Exercice I
c
EL H
O U
Créer une nouvelle classe d’exception AdresseException pour
L M
fusionner et remplacer les deux exceptions
e
IncorrectCodePostalException
r f E et
c h
IncorrectStreetNameException
A
c
Java
Contenu de la classe AdresseException
package org.eclipse.exception;
}
majuscule.");
f E L
c hre
c A
public AdresseException(int index, String value) {
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 être en
majuscule.");
}
}
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");
}
I c
catch (AdresseException e) {
ELH
e.printStackTrace(); U
}
L MO
}
f E
c hre
c A
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");
}
I c
catch (AdresseException e) {
ELH
e.printStackTrace(); U
}
L MO
}
f E
A c hre
Le message
c
affiché est :
Java
I c
H
EL qu’une exception
U
soit levée ou non L MO
À utiliser quand on veut exécuter une instruction
h r e fE
A c
c
Java
Exemple
public class Test {
public static void main(String[] args) {
int x = 5, y = 0;
try {
I c
System.out.println(x / y);
ELH
U
}
L MO
catch (Exception e) {
f E
hre
System.out.println("Division par zéro");
c
c A
}
finally{
System.out.println("Instruction exécutée systé
matiquement");
}
}
}
H & H: Research and Training 32 / 33
Le bloc finally
Java
I
c
Remarque
E LH
U
return qui forcera l’arrêt de l’exL MOdu sicode.
Le bloc finally peut s’avérer intéressant le catch contient un
re
(finally) sera exécuté. f E écution Malgré cela, ce bloc
c h
c A