• Aucun résultat trouvé

iUT ORSAY

N/A
N/A
Protected

Academic year: 2022

Partager "iUT ORSAY"

Copied!
4
0
0

Texte intégral

(1)

iUT ORSAY DUT Informatique Module JAVA Apprentis

Département Informatique 2008 / 2009

Travaux Dirigés n

o

4 : Exceptions

Objectifs : Comprendre le concept et l’utilité du mécanisme d’exception. Sa- voir gérer les exceptions standard et créer, lancer, rattraper ses propres excep- tions.

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).

/∗ ∗

C l a s s e E x e r c i c e 1 d e s t i n é e à m a n i p u l e r l e s n p r e m i e r s e n t i e r s ( n é t a n t p a s s é

en p a r a m è t r e du programme ) .

@author Monprenom Monnom

/

p u b l i c c l a s s E x e r c i c e 1 {

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] ; p u b l i c E x e r c i c e 1 ( i n t n ) {

f o r ( i n t i = 0 ; i <n ; i ++) t a b l e a u [ i ] = i ; }

p u b l i c i n t sommeDesNPremiers ( i n t n ) { i n t 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 ] ; r e t u r n 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 ) ; i n t n = I n t e g e r . p a r s e I n t ( a r g s [ 0 ] ) ; i n t r e s u l t a t ;

r e s u l t a t = ex1 . sommeDesNPrem iers ( n ) ;

S ys tem . 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 nom bres " ) ; S ys tem . o u t . p r i n t l n ( " e s t : " + r e s u l t a t ) ;

} }

(2)

Travaux Dirigés no4 Exceptions 2/4

2 Exercice 2 : gestionnaires d’exceptions

Soit le programme Java suivant :

p u b l i c c l a s s E x e r c i c e 2 {

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 [ ] ) { i n t k ;

t r y {

k = 1 / I n t e g e r . p a r s e I n t ( a r g s [ 0 ] ) ; }

c a t c h ( R u n t i m e E x c e p t i o n ex ) {

S ys tem . e r r . p r i n t l n ( " Runtim e " + 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 ) { S ys tem . 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 ) {

S ys tem . 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 :

imp ort j a v a . i o .∗; c l a s s E x e r c i c e 3 {

F i l e 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 ) ; }

p u b l i c v o i d c r e e r F i c ( ) { f . c r e a t e N e w F i l e ( ) ; }

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

(3)

Travaux Dirigés no4 Exceptions 3/4

– Proposition 1 :

imp ort j a v a . i o .∗; c l a s s E x e r c i c e 3 p 1 {

F i l e 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 ) ; }

p u b l i c v o i d c r e e r F i c ( ) { t r y {

f . c r e a t e N e w F i l e ( ) ; }

c a t c h ( I O E x c e p t i o n ex ) { S ys tem . e r r . p r i n t l n ( ex ) ; S ys tem . 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 :

imp ort j a v a . i o .∗; c l a s s E x e r c i c e 3 p 2 {

F i l e 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 ( ) th row s I O E x c e p t i o n { f . c r e a t e N e w F i l e ( ) ;

}

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 :

imp ort j a v a . i o .∗; c l a s s E x e r c i c e 3 p 3 {

F i l e 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 ) ; }

p u b l i c v o i d c r e e r F i c ( ) { f . c r e a t e N e w F i l e ( ) ; }

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 ] ) ; t r y {

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 ) { S ys tem . e r r . p r i n t l n ( ex ) ; S ys tem . e x i t ( 3 ) ;

} }

}

IUT d’Orsay – DUT Informatique 2008 / 2009 Module JAVA Apprentis

(4)

Travaux Dirigés no4 Exceptions 4/4

– Proposition 4 :

imp ort j a v a . i o .∗; c l a s s E x e r c i c e 3 p 4 {

F i l e 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 ) ; }

p u b l i c v o i d c r e e r F i c ( ) { t r y {

f . c r e a t e N e w F i l e ( ) ; }

c a t c h ( I O E x c e p t i o n ex ) { S ys tem . e r r . p r i n t l n ( ex ) ; S ys tem . 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 ] ) ; t r y {

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 ) { S ys tem . e r r . p r i n t l n ( ex ) ; S ys tem . e x i t ( 3 ) ;

} }

}

– Proposition 5 :

imp ort j a v a . i o .∗; c l a s s E x e r c i c e 3 p 5 {

F i l e 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 ) ; }

p u b l i c v o i d c r e e r F i c ( ) { t r y {

f . c r e a t e N e w F i l e ( ) ; }

c a t c h ( E x c e p t i o n ex ) {

S ys tem . e r r . p r i n t l n ( ex ) ; S ys tem . e x i t ( 5 ) ;

}

c a t c h ( I O E x c e p t i o n ex ) { S ys tem . e r r . p r i n t l n ( ex ) ; S ys tem . 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

Références

Documents relatifs

La liste des exceptions possible est indiquée par throws suivi des classes d'exceptions concernées. public static int parseInt(String s) throws

Lorsqu’on utilise une suite d’instructions qui est susceptible de lever une exception : soit on capture l’exception et on ex´ ecute une suite d’instructions qui a pour objectif

Son travail est de vérifier la validité d’un numéro de carte banquaire : les clients lui envoient trois nombres, un premier de type long long int pour le numéro de carte, un second

- Lorsqu’une exception est levée, l’exécution normale du programme s’arrête et on saute toutes les instructions jusqu’à ce que l’exception soit rattrapée ou jusqu’à ce

„ Le contenu d'une classe exception peut être vide, dans ce cas seul le nom de la classe importe.. „ Il peut aussi permettre s'il est non vide de gérer des informations sur

Le programme qui suit lance une ArithmeticException avec le message &#34;Mauvais calcul !&#34; dans la méthode meth() et intercepte cette exception dans le bloc englobant main..

Le code précédent ne marche pas bien car close() peut aussi lever une exception qui peut masquer l’exception lancer dans le try. try(BufferedReader

● Si l'on appel une méthode qui lève une exception non runtime.. – Catch si l'on peut reprendre sur l'erreur et faire quelque chose de