[Débutant] Intérêt et différence de throws/throw
Bonjour,
Je ne comprends pas vraiment l'intérêt de throws/throw. Par exemple, si j'ai ça :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class X {
public static void main(String[] a) {
int i = 10, j = 0;
try {
System.out.println(Y.m(i,j));
}
catch (ArithmeticException e) {
System.out.println("catched");
}
}
}
class Y {
static double m(int i, int j) {
return i/j;
}
} |
... quel et l'intérêt d'écrire :
Code:
static double m(int i, int j) throws ArithmeticException {
ou encore :
Code:
1 2 3 4 5 6 7 8
| class Y {
static double m(int i, int j) {
if (j == 0) throw new ArithmeticException();
return i/j;
}
} |
J'ai cru comprendre aussi qu'il fallait mettre throws dans la méthode susceptible de générer l'erreur et throw à l'endroit où l'erreur est détectée, mais pour moi ça n'est qu'une différence de syntaxe. Quelle est la différence "conceptuelle" (pour ainsi dire) entre throw et throws ?
Merci pour vos éclaircissements (les exams sont la semaine prochaine ^^)
++