Exceptions
Transcription
Exceptions
iUT ORSAY Département Informatique DUT Informatique Module JAVA Apprentis 2008 / 2009 Travaux Dirigés no 4 : Exceptions Objectifs : Comprendre le concept et l’utilité du mécanisme d’exception. Savoir gérer les exceptions standard et créer, lancer, rattraper ses propres exceptions. 1 Exercice 1 : exceptions standard Le programme fourni ci-dessous ne gère pas les erreurs qui peuvent se produire lors de son lancement. 1. Lister les erreurs d’exécution possibles dues à un lancement erroné. 2. Proposer des modifications du programme permettant de gérer toutes ces erreurs (en utilisant uniquement la gestion des exceptions). /∗ ∗ ∗ Classe Exercice1 de s t in é e à manipuler l e s n premiers e n t i e r s ( n ét ant passé ∗ en p a r a m è t r e du programme ) . ∗ @author Monprenom Monnom ∗/ public c l as s Exercice1 { p r i v a t e f i n a l i n t MAX = 9 9 ; p r i v a t e i n t t a b l e a u [ ] = new i n t [MAX] ; public Exercice1 ( in t n ) { f o r ( i n t i = 0 ; i <n ; i ++) tableau [ i ] = i ; } p u b l i c i n t sommeDesNPremiers ( i n t n ) { int r e s u l t a t = 0; f o r ( i n t i = 0 ; i <n ; i ++) r e s u l t a t += t a b l e a u [ i ] ; return r e s u l t a t ; } p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) { E x e r c i c e 1 ex1 = new E x e r c i c e 1 ( 1 0 ) ; int n = Integer . parseInt ( args [ 0 ] ) ; int r es u ltat ; r e s u l t a t = ex1 . sommeDesNPremiers ( n ) ; System . o u t . p r i n t ( " La somme d e s " + a r g s [ 0 ] + " p r e m i e r s nombres " ) ; System . o u t . p r i n t l n ( " e s t : " + r e s u l t a t ) ; } } Travaux Dirigés no 4 2 Exceptions 2/4 Exercice 2 : gestionnaires d’exceptions Soit le programme Java suivant : public cl as s Exercice2 { p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) { int k ; try { k = 1/ In t eg e r . p a r se I n t ( args [ 0 ] ) ; } c a t c h ( R u n t i m e E x c e p t i o n ex ) { System . e r r . p r i n t l n ( " Runtime " +ex ) ; } c a t c h ( I n d e x O u t O f B o u n d s E x c e p t i o n ex ) { System . e r r . p r i n t l n ( " I n d e x " + ex ) ; } c a t c h ( A r i t h m e t i c E x c e p t i o n ex ) { System . e r r . p r i n t l n ( " I n d e x " + ex ) ; } } } Lors de la compilation du programme, nous obtenons les messages d’erreur suivants : Exercice2.java:11: exception java.lang.IndexOutOfBoundsException has already been caught catch (IndexOutOfBoundsException ex) { ^ Exercice2.java:14: exception java.lang.ArithmeticException has already been caught catch (ArithmeticException ex) { ^ 2 errors Expliquez le problème et proposez une solution. 3 Exercice 3 Soit le programme Java suivant : import j a v a . i o . ∗ ; c las s Exercice3 { File f ; p u b l i c E x e r c i c e 3 ( S t r i n g nomFic ) { f = new F i l e ( nomFic ) ; } public void c r e e r F i c ( ) { f . createNewFile ( ) ; } p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) { E x e r c i c e 3 ex3 = new E x e r c i c e 3 ( a r g s [ 0 ] ) ; ex3 . c r e e r F i c ( ) ; } } Lors de la compilation du programme, nous obtenons le message d’erreur suivant : Exercice3.java:9: unreported exception java.io.IOException; must be caught or declared to be thrown f.createNewFile(); ^ 1 error Indiquez pour chacune des propositions de correction ci-dessous si elle est valide, non valide ou dépend du contexte. Justifiez chaque réponse. IUT d’Orsay – DUT Informatique 2008 / 2009 Module JAVA Apprentis Travaux Dirigés no 4 Exceptions 3/4 – Proposition 1 : import j a v a . i o . ∗ ; c la s s Exercice3p1 { File f ; p u b l i c E x e r c i c e 3 p 1 ( S t r i n g nomFic ) { f = new F i l e ( nomFic ) ; } public void c r e e r F i c ( ) { try { f . createNewFile ( ) ; } c a t c h ( I O E x c e p t i o n ex ) { System . e r r . p r i n t l n ( ex ) ; System . e x i t ( 3 ) ; } } p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) { E x e r c i c e 3 p 1 ex3 = new E x e r c i c e 3 p 1 ( a r g s [ 0 ] ) ; ex3 . c r e e r F i c ( ) ; } } – Proposition 2 : import j a v a . i o . ∗ ; c la s s Exercice3p2 { File f ; p u b l i c E x e r c i c e 3 p 2 ( S t r i n g nomFic ) { f = new F i l e ( nomFic ) ; } p u b l i c v o i d c r e e r F i c ( ) throws I O E x c e p t i o n { f . createNewFile ( ) ; } p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) { E x e r c i c e 3 p 2 ex3 = new E x e r c i c e 3 p 2 ( a r g s [ 0 ] ) ; ex3 . c r e e r F i c ( ) ; } } – Proposition 3 : import j a v a . i o . ∗ ; c la s s Exercice3p3 { File f ; p u b l i c E x e r c i c e 3 p 3 ( S t r i n g nomFic ) { f = new F i l e ( nomFic ) ; } public void c r e e r F i c ( ) { f . createNewFile ( ) ; } p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) { E x e r c i c e 3 p 3 ex3 = new E x e r c i c e 3 p 3 ( a r g s [ 0 ] ) ; try { ex3 . c r e e r F i c ( ) ; } c a t c h ( I O E x c e p t i o n ex ) { System . e r r . p r i n t l n ( ex ) ; System . e x i t ( 3 ) ; } } } IUT d’Orsay – DUT Informatique 2008 / 2009 Module JAVA Apprentis Travaux Dirigés no 4 Exceptions 4/4 – Proposition 4 : import j a v a . i o . ∗ ; c la s s Exercice3p4 { File f ; p u b l i c E x e r c i c e 3 p 4 ( S t r i n g nomFic ) { f = new F i l e ( nomFic ) ; } public void c r e e r F i c ( ) { try { f . createNewFile ( ) ; } c a t c h ( I O E x c e p t i o n ex ) { System . e r r . p r i n t l n ( ex ) ; System . e x i t ( 3 ) ; } } p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) { E x e r c i c e 3 p 4 ex3 = new E x e r c i c e 3 p 4 ( a r g s [ 0 ] ) ; try { ex3 . c r e e r F i c ( ) ; } c a t c h ( I O E x c e p t i o n ex ) { System . e r r . p r i n t l n ( ex ) ; System . e x i t ( 3 ) ; } } } – Proposition 5 : import j a v a . i o . ∗ ; c la s s Exercice3p5 { File f ; p u b l i c E x e r c i c e 3 p 5 ( S t r i n g nomFic ) { f = new F i l e ( nomFic ) ; } public void c r e e r F i c ( ) { try { f . createNewFile ( ) ; } c a t c h ( E x c e p t i o n ex ) { System . e r r . p r i n t l n ( ex ) ; System . e x i t ( 5 ) ; } c a t c h ( I O E x c e p t i o n ex ) { System . e r r . p r i n t l n ( ex ) ; System . e x i t ( 3 ) ; } } p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) { E x e r c i c e 3 p 5 ex3 = new E x e r c i c e 3 p 5 ( a r g s [ 0 ] ) ; ex3 . c r e e r F i c ( ) ; } } 4 Exercice 4 : exceptions utilisateur Reprendre le code Java donné dans l’exercice 1 en ajoutant une exception personnelle nommée NombreDeNombresException. Cette exception sera levée dans le cas où le paramètre passé à la méthode sommeDesNPremiers est négatif ou nul. IUT d’Orsay – DUT Informatique 2008 / 2009 Module JAVA Apprentis