Les Exceptions ,Try Catch
Voici un code ou je gère les exceptions dans le cas d'une saisit d'un indice qui n'est pas compris dans les bornes du tableau,en cas de divison par zéro.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
class exo11_1 {
static int [] tableau={17,12,15,38,29,157,89,-22,0,5 };
static int division(int indice,int diviseur) {
return tableau[indice] / diviseur ;
}
public static void main(String[] args ) {
int x,y;
do {
try {
Terminal.ecrireStringln("Entrez l'indice de l'entier a diviser ");
x=Terminal.lireInt();
if(x<0 || x > tableau.length ) {
throw new ArrayIndexOutOfBoundsException();
}
}catch(ArrayIndexOutOfBoundsException e) {
Terminal.ecrireStringln("Entrez l'indice de l'entier a diviser ");
x=Terminal.lireInt();
}
} while(x < 0 || x > tableau.length);
do {
try {
Terminal.ecrireString("entrez le diviseur ");
y=Terminal.lireInt();
if(y==0) {
throw new ArithmeticException();
}
} catch (ArithmeticException a ) {
Terminal.ecrireStringln("entrez le diviseur");
y=Terminal.lireInt();
}
} while(y == 0);
Terminal.ecrireStringln("le resultat de la division est ");
Terminal.ecrireIntln(division(x,y));
}
} |
Ce que je n'arrive pas a géré c'est dans le cas ou l'utilisateur entre autre chose qu'un entier, genre un "char " par exemple.
Dans mon if(Comment exprimé en java si la saisit n'est pas entier alors je leve l'exception ...
Merci