Correction TP: Gestion des Exceptions en JAVA

Java Programming · notes

Browse all programmation documents

Correction TP

Gestion des exceptions en JAVA

Exercice n°1 :

[Initiation au traitement des exceptions]

import java.util.InputMismatchException;

import java.util.Scanner;

public class exceptionTest {

public static void main(String[] args) {

int a, b, res;

Scanner clavier = new Scanner(System.in);

try{

System.out.println("a =");

a = clavier.nextInt();

System.out.println("b =");

b = clavier.nextInt();

res = a / b;

System.out.println(a + " / " + b + " = " + res);

}

catch(ArithmeticException e){

System.out.println("oops ! un problème dans la

division");

System.out.println ("le message officiel est " +

e.getMessage()) ;

}

catch(InputMismatchException e){

System.out.println("Erreur de type"+e.getMessage());

}

finally {

exécuté") ;

System.out.println("le bloc finally sera toujours

System.out.println("et c'est là que l'on fermera par

exemple les fichiers") ;

}

clavier.close();

System.out.println ("Fin du programme") ;

}

}

CHALOUAH Anissa

Advertisement

1

Exercice n°2 :

[Utilisation des Exceptions]

class Somme {

public static void main(String[] args) {

int somme = 0;

for(int i=0;i<args.length;i++) {

try {

somme += Integer.parseInt(args[i]);

}

catch (NumberFormatException e) {}

}

System.out.println(somme);

}

}

Exercice n°3 :

[Exceptions personnalisées et constructeurs]

public class adresseIP {

private int[] octet;

public adresseIP (int o1,int o2, int o3, int o4) throws

ExceptionAdrIP {

if (o1 < 0 || o1 > 255)

throw new ExceptionAdrIP("valeur incorrecte pour le

premier octet");

if (o2 < 0 || o2 > 255)

throw new ExceptionAdrIP("valeur incorrecte pour le

deuxième octet");

if (o3 < 0 || o3 > 255)

throw new ExceptionAdrIP("valeur incorrecte pour le

troisième octet");

if (o4 < 0 || o4 > 255)

quatrième octet");

throw new ExceptionAdrIP("valeur incorrecte pour le

octet = new int[] {o1,o2,o3,o4};

}

public String toString(){

return octet[0] + "." + octet[1] + "." + octet[2] + "."+ octet[3]

;

Advertisement

}

public static void main(String[] args) {

adresseIP adr;

try {

adr = new adresseIP(255,168,1929,10);

System.out.println(adr);

} catch (ExceptionAdrIP e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

CHALOUAH Anissa

2

System.out.println ("Fin du programme") ;}

}

class ExceptionAdrIP extends Exception{

public ExceptionAdrIP(String s){

super(s);

}

}

Exercice n°4 :

[Exceptions personnalisées & traitement séparé]

public class WrongInputLengthException extends Exception{

public String toString(){

return "Trop de caractères en entrée";

}

}

public class WrongLoginException extends Exception{

public String toString(){

return "login inexistant";

}

}

public class WrongPwdException extends Exception{

public String toString(){

return "Mauvais mot de passe";

}

}

import java.io.IOException;

import java.util.Scanner;

Advertisement

public class Authentification {

static final String LoginCorrect="scott";

static final String PwdCorrect="tiger";

public void getUserLogin() throws WrongLoginException,

WrongInputLengthException,IOException{

System.out.println("Entrer Login : ");

Scanner sc=new Scanner(System.in);

String lg=sc.nextLine();

if (lg.length()>8){

throw new WrongInputLengthException();

}

CHALOUAH Anissa

3

if (!lg.trim().equals(LoginCorrect)){

throw new WrongLoginException();

}

}

public void getUserPassword() throws WrongPwdException,

WrongInputLengthException,IOException{

System.out.println("Entrer password : ");

Scanner sc=new Scanner(System.in);

String pw=sc.nextLine();

if (pw.length()>8){

throw new WrongInputLengthException();

}

if (!pw.trim().equals(PwdCorrect)){

throw new WrongPwdException();

}

}

public Authentification()throws WrongLoginException, WrongPwdException,

WrongInputLengthException,IOException {

this.getUserLogin();

this.getUserPassword();

}

}

import java.io.IOException;

public class Test {

public static void main(String[] args) {

Advertisement

// TODO Auto-generated method stub

boolean erreur=true;

while(erreur){

try {

new Authentification();

erreur=false;

System.out.println("Login/mot de passe

} catch(WrongLoginException wle){

System.out.println(wle);

}

catch(WrongPwdException wpe){

System.out.println(wpe);

}

catch(WrongInputLengthException wile){

System.out.println(wile);

System.exit(0); //risque d'attaque, on sort du

}

catch(IOException ioe){

ioe.printStackTrace();

System.exit(0); //risque d'attaque, erreur

}

catch(Exception e){

e.printStackTrace();

4

valides");

programme

d'entrée/sortie

CHALOUAH Anissa

prendre de risque, on sort

System.exit(0); //Pbm inconnu, pour ne pas

}

System.out.println("okkkkkkk");

}}

CHALOUAH Anissa

5