Source de Equation1.java
public class Equation1 { private int a, b; public static void main(String args[]) { int valeurA=Integer.valueOf(args[0]).intValue(); int valeurB=Integer.valueOf(args[1]).intValue(); Equation1 equa = new Equation1(valeurA,valeurB); equa.afficher(); int x = equa.solution(); System.out.println("résultat : X = "+x); } public Equation1(int a, int b) { this.a=a; this.b=b; } public void afficher() { System.out.println(a+" * X = "+b); } int solution() { return b/a; } } |
java Equation1 0 2 0 * X = 2 Exception in thread "main" java.lang.ArithmeticException: / by zero at Equation1.solution(Equation1.java:20) at Equation1.main(Equation1.java:10) |
Source de Equation2.java
public class Equation2 { private int a, b; public static void main(String args[]) { int valeurA=Integer.valueOf(args[0]).intValue(); int valeurB=Integer.valueOf(args[1]).intValue(); Equation2 equa = new Equation2(valeurA,valeurB); equa.afficher(); int x = equa.solution(); System.out.println("résultat : X = "+x); } public Equation2(int a, int b) { this.a=a; this.b=b; } public void afficher() { System.out.println(a+" * X = "+b); } int solution() { int x; try { x = b/a; } catch (ArithmeticException e) { x = -1; } return x; } } |
java Equation2 0 2 0 * X = 2 résultat : X = -1 |
Source de Equation3.java
public class Equation3 { private int a, b; public static void main(String args[]) { int valeurA=Integer.valueOf(args[0]).intValue(); int valeurB=Integer.valueOf(args[1]).intValue(); Equation3 equa = new Equation3(valeurA,valeurB); equa.afficher(); int x = equa.solution(); System.out.println("résultat : X = "+x); } public Equation3(int a, int b) { this.a=a; this.b=b; } public void afficher() { System.out.println(a+" * X = "+b); } int solution() throws ArithmeticException { return b/a; } } |
Source de Prog53.java
public class Prog53 { public static void main(String args[]) { int valeurA=Integer.valueOf(args[0]).intValue(); int valeurB=Integer.valueOf(args[1]).intValue(); Equation3 equa = new Equation3(valeurA,valeurB); equa.afficher(); try { int x = equa.solution(); System.out.println("résultat : X = "+x); } catch (ArithmeticException e) { System.out.println("pas de solution"); } } } |
java Prog53 0 2 0 * X = 2 pas de solution |
Source de Equation4.java
public class Equation4 { private int a, b; public static void main(String args[]) { int valeurA=Integer.valueOf(args[0]).intValue(); int valeurB=Integer.valueOf(args[1]).intValue(); Equation4 equa = new Equation4(valeurA,valeurB); equa.afficher(); int x = equa.solution(); System.out.println("résultat : X = "+x); } public Equation4(int a, int b) { this.a=a; this.b=b; } public void afficher() { System.out.println(a+" * X = "+b); } int solution() throws ArithmeticException { if (a==0) throw new ArithmeticException( "division entière par zéro"); else return b/a; } } |
Source de Prog54.java
public class Prog54 { public static void main(String args[]) { int valeurA=Integer.valueOf(args[0]).intValue(); int valeurB=Integer.valueOf(args[1]).intValue(); Equation4 equa = new Equation4(valeurA,valeurB); equa.afficher(); try { int x = equa.solution(); System.out.println("résultat : X = "+x); } catch (ArithmeticException e) { System.out.println(e.getMessage()); } } } |
java Prog54 0 2 0 * X = 2 division entière par zéro |
try { bloc d'instructions
pouvant lever des exceptions
} catch { type_d_exception e) {
traitement de e
} catch {autre_type_d_exception e) {
traitement de e
}
[<modificateur>] <type>
<nomMéthode> ([<paramètres>]) throws
<liste d'exceptions> { <corps de la méthode pouvant
lever des exceptions> }