Exception

erreur de division

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;
  }
}

capturer l'exception

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;
  }
}

propager l'exception

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");
    } 
  }
}

lever une exception

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());
    } 
  }
}

classe Exception

exercices