salut ,

J ' ai un petit souci en ce qui concerne la propagation et le traitement des exceptions.
Dans le programme qui suit ,il s 'agit de calcul d' une matrice , je dois cependant lever une exception dans les 2 cas suivants :

- Soustraction : la matrice ne doit pas accepter une valeur négative
-Division : je ne peux diviser par Zero

Or , lorsqu' une méthode appelle une méthode capable de lever une exception , comment l' indiquer , qui catch cette exception?

Voici le code source :


Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
public class Matrice {
 
   private int [] [] m ; 
 
   public Matrice(int n ) {
        m = new int [n][n];
 
   }
 
   public void afficher (){
       for (int i = 0 ;i <=m.length -1 ; i ++ ){
           for (int j = 0 ; j<=m[i].length -1 ;j++){
               System.out.print (m[i][j]+ " ");
 
           }
           System.out.println ();
       }
 
   }
 
   private void operation (int ligne , int colonne , char operation , int val) throws MatriceException{
 
       if (operation == '+'){
           m[ligne][colonne] =m[ligne][colonne]+ val;
 
      }
 
 
        if (operation == '-'){
 
                //if  (m[ligne][colonne]- val <0 ) throw new SoustractionException ("la matrice ne peut contenir de nombres négatifs");
           m[ligne][colonne] =m[ligne][colonne]- val;
           if  (m[ligne][colonne]<0 ) throw new SoustractionException ("la matrice ne peut contenir de nombres négatifs");
 
 
        }
 
 
   }
 
 
 
 
   private void operations (char operation , int val) {
     //try {   
       for (int  i =0 ; i<=m.length -1 ; i ++ ){
           for ( int j  = 0 ; j<=m[i].length -1 ;j++){
 
               this.operation(i ,j ,operation , val);
           }
    }
  // }catch (MatriceException e ){
       //System.out.println (" Exception dans methode operations " + e.getMessage());
 
   //}
   }
 
 
 
   public void soustraction (int nombre ) throws SoustractionException {
 
      this.operations ('-' ,nombre );
 
 
 
 
   }
    public void multiplication (int nombre) {
 
   }
     public void division(int nombre) throws DivisionException {
       this.operations ('/' ,nombre );
   }
 
   public void addition (int nombre)  {
       this.operations ('+' ,nombre );
 
 
   }
 
}
Merci