Professional Documents
Culture Documents
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Plan
1
Introduction aux exceptions en Java Exemples de code avec gestion des exceptions Crer ses propres exceptions Instruction try/catch/nally java.lang.Error et java.lang.RuntimeException Avantages lis aux exceptions
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Lobjet Exception
Lorsquune erreur se produit dans une mthode, celle-ci cre un objet Exception qui contient des informations sur lerreur survenue :
le type de lerreur : objet null, dpassement de tableau, etc. la trace dexcution du programme : la stack trace
Laction de crer un objet Exception et de le passer au systme dexcution est appele lever/lancer une exception (throwing an exception)
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Rcupration de lexception
Lorsquune exception est leve, le systme dexcution tente de trouver un handler pour cette exception : c--d. une mthode pouvant la grer. Pour cela, il cherchera dans les mthodes qui ont t appeles jusqu lexception : the call stack (la pile dappels)
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Exemples
Le constructeur de la classe java.io.PrintWriter est susceptible, par dnition (cf. api), de renvoyer une exception du type java.io.IOException. Cest pourquoi le code suivant ne peut pas tre compil. Il ne respecte pas le catch or specify requirement ! Exemple de code erron
package prog ; import java . io . F i l e W r i t e r ; import java . io . P r i n t W r i t e r ; public class TestingExceptions { public void w r i t e F i l e T e s t i n g ( ) { P r i n t W r i t e r o u t = new P r i n t W r i t e r ( new F i l e W r i t e r ( " O u t F i l e . t x t " ) ) ; out . p r i n t ( " t e s t " ) ; } }
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Remarque
Prfrez la solution 1. Il est toujours plus sr de grer soi-mme les exceptions. Ici la gestion est dlgue la mthode appelante.
Cours Java - F. Michel 13 / 39
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Il existe deux sous-classes prdnies dans le JDK : java.lang.Error et java.lang.Exception les exceptions "classiques" sont des sous-classes de java.lang.Exception les exceptions sous-classes de java.lang.Error stoppent un programme lorsquelles sont lances. (on ne sen sert que trs rarement).
Cours Java - F. Michel 15 / 39
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
grer, par programmation (et non par message derreur), les utilisations incorrectes dune mthode. et surtout prvenir lutilisateur de la mthode que certaines utilisations sont incorrectes et quelles doivent tre gres explicitement.
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
NegativeAgeException.java
package prog ; p u b l i c c l a s s NegativeAgeException extends E x c ep t i o n { }
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Personne.java
. . . p u b l i c v o i d setAge ( i n t age ) throws NegativeAgeException { i f ( age < 0 ) { throw ( new NegativeAgeException ( ) ) ; / / s o r t de l a methode } t h i s . age = age ; / / execute seulement s i age >= 0 } . . .
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Excution
TestPersonne.java
package prog ; p u b l i c c l a s s TestPersonne { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args ) { Personne pers = new Personne ( 1 0 ) ; try { pers . setAge ( 1); } c a t c h ( NegativeAgeException e ) { e . printStackTrace ( ) ; } } }
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
java.lang.Exception
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
La classe java.lang.Exception possde un constructeur qui permet dinitialiser le message qui sera obtenu : NegativeAgeException.java
package prog ; p u b l i c c l a s s NegativeAgeException extends E x c ep t i o n { p u b l i c NegativeAgeException ( S t r i n g message ) { super ( message ) ; } }
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Excution
TestPersonne.java
package prog ; p u b l i c c l a s s TestPersonne { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args ) { Personne pers = new Personne ( 1 0 ) ; try { pers . setAge ( 1); } c a t c h ( NegativeAgeException e ) { e . printStackTrace ( ) ; } } }
Rsultat :
prog . NegativeAgeException : e r r e u r : l age d o i t e t r e > 0 , v a l e u r e n t r e e : 1 a t prog . Personne . setAge ( Personne . j a v a : 1 3 ) a t prog . TestPersonne . main ( TestPersonne . j a v a : 8 )
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Instruction try/catch/nally
Le bloc try
try { code } c a t c h and f i n a l l y b l o c k s . . .
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Instruction try/catch/nally
Les blocs catch
try { } c a t c h ( ExceptionType name ) { } c a t c h ( AnotherExceptionType name ) { }
Exemple
try { . . . } c a t c h ( F il e N o t F o u n d E x c e p t i o n e ) { System . e r r . p r i n t l n ( " F i l e N o t F o u n d E x c e p t i o n : " + e . getMessage ( ) ) ; } c a t c h ( IOException e ) { System . e r r . p r i n t l n ( " Caught IOException : " + e . getMessage ( ) ) ; } Cours Java - F. Michel 28 / 39
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Instruction try/catch/nally
Le block nally est un block optionnel contenant des instructions qui seront toujours excutes quelque soit le rsultat du try : Exemple pour une ouverture de chier
try { code } catch { code } finally { i f ( out != n u l l ) { System . o u t . p r i n t l n ( " C l o s i n g P r i n t W r i t e r " ) ; out . close ( ) ; } else { System . o u t . p r i n t l n ( " P r i n t W r i t e r n o t open " ) ; } }
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Instruction try/catch/nally
Il permet donc dviter dcrire plusieurs fois le mme code. Notamment, il permet de sassurer que les ressources ventuellement utilises sont toujours libres : Exemple sans nally (code redondant)
try { ... out . close ( ) ; / / Don t do t h i s ; i t d u p l i c a t e s code . } c a t c h ( F ile Not Fou ndE xce pti on e ) { out . close ( ) ; / / Don t do t h i s ; i t d u p l i c a t e s code . System . e r r . p r i n t l n ( " Caught : F ile Not Fou ndE xce pti on : " + e . getMessage ( ) ) ; } c a t c h ( IOException e ) { out . close ( ) ; / / Don t do t h i s ; i t d u p l i c a t e s code . System . e r r . p r i n t l n ( " Caught " +e . getMessage ( ) ) ; }
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Toutes ces instructions sont potentiellement source de problme ; il faut pouvoir les grer.
Cours Java - F. Michel 37 / 39
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages
Introduction
Exemples
Cration
Instruction try/catch/nally
Error et RuntimeException
Avantages