Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème de gestion de plusieurs méthodes dans try

    9 novembre 2008 à 0:27:31

    Bonsoir,

    Je dois créer un petit projet pour l'école.
    Ce la concerne la gestion d'exception.
    Je dois créer des étudiants avec une liste de cours maximum à 10 et créer une méthode qui permet de récupérer un cours selon un indice qui correspond à l'indice dans mon tableau.

    Mon problème est qu'il gère la 1ère méthode mon " try " et pas les autres.
    Il gère correctement la 1ère erreur mais s'arrête là, comme si il ne voulait pas lire les autres méthodes.

    Test.java ( contenant le main )
    package packClasses;
    
    import packExceptions.*;
    
    public class Test {
    
    	public static void main(String[] args) {
    		String[] cours = new String[10];
    		
    		Etudiant e1 = new Etudiant("Valdevit", "John", cours);
    		
    		e1.ajouterCours("Système d'exploitation");
    		e1.ajouterCours("C");
    		System.out.println("Nombre de cours : " + e1.compteNbCours() + "\n");
    		
    		try{
    			System.out.println(e1.getCours(5) + "\n");
    			System.out.println(e1.getCours(-1) + "\n");
    			System.out.println(e1.getCours(12) + "\n");
    			System.out.println(e1.getCours(1) + "\n");
    
    		}
    		catch(Exception e){
    			System.out.println(e.getMessage()); // Affiche la valeur incorrecte
    		}
    /*		catch(ExceptionIndiceNegatif e){
    			System.out.println(e.getMessage()); // Affiche la valeur incorrecte
    		}
    		catch(ExceptionIndiceSup10 e){
    			System.out.println(e.getMessage()); // Affiche la valeur incorrecte
    		}
    		catch(ExceptionIndiceSupCoursSuivis e){
    			System.out.println(e.getMessage()); // Affiche la valeur incorrecte
    		}
    	*/	
    		System.out.println("Bouh !");
    			
    			
    
    	}
    
    }
    


    Etudiant.java
    package packClasses;
    
    import packExceptions.ExceptionIndiceNegatif;
    import packExceptions.ExceptionIndiceSup10;
    import packExceptions.ExceptionIndiceSupCoursSuivis;
    
    public class Etudiant {
    
    	private String nom;
    	private String prenom;
    	private String cours[] = new String[10];
    	
    	public Etudiant(String nom, String prenom, String[] cours) {
    		setNom(nom);
    		setPrenom(prenom);
    		setCours(cours);
    	}
    
    	public void ajouterCours(String nomCours) {
    		for (int i=0 ; i<=cours.length ; i++){
    			if (cours[i] == null){
    				cours[i] = nomCours;
    				break;
    			}
    		}
    	}
    	
    	public int compteNbCours(){
    		int nb = 0;
    		for (int i=0 ; i <= cours.length ; i++){
    			if (cours[i] != null)
    				continue;
    			else{
    				nb = i;
    				break;
    			}
    		}
    		return nb;	
    	}
    
    	public String getCours(int id) throws ExceptionIndiceNegatif, ExceptionIndiceSupCoursSuivis, ExceptionIndiceSup10{
    		try{
    			if (id<0) throw new ExceptionIndiceNegatif(id);
    			if (id>9) throw new ExceptionIndiceSup10(id);
    			if (id>compteNbCours()-1) throw new ExceptionIndiceSupCoursSuivis(id);
    		}
    		catch(ExceptionIndiceNegatif eIN){
    			System.out.println(eIN.getMessage());
    		} 
    		catch (ExceptionIndiceSup10 eIS10) {
    			System.out.println(eIS10.getMessage());
    		}
    		catch (ExceptionIndiceSupCoursSuivis eISCS) {
    			System.out.println(eISCS.getMessage());
    		}
    		
    		return cours[id];	
    	}
    	
    	public void setCours(String[] cours) {
    		this.cours = cours;
    	}
    
    	public void setNom(String nom) {
    		this.nom = nom;
    	}
    
    	public void setPrenom(String prenom) {
    		this.prenom = prenom;
    	}
    }
    


    ExceptionIndiceNegatif.java
    package packExceptions;
    
    public class ExceptionIndiceNegatif extends Exception{
    
    	private static final long serialVersionUID = 3263619907048120866L;
    	private int id;
    	
    	public ExceptionIndiceNegatif(int id) {
    		this.id = id;
    	}
    
    	public String getMessage() {
    		return "Le numéro du cours est négatif";
    	}
    
    }
    


    </code>
    
    ExceptionIndiceSup10.java
    <code type="java">
    package packExceptions;
    
    public class ExceptionIndiceSup10 extends Exception {
    
    	private static final long serialVersionUID = 1107667958834970714L;
    	private int id;
    	
    	public ExceptionIndiceSup10(int id) {
    		this.id = id;
    	}
    
    	public String getMessage() {
    		return "Le numéro du cours est supérieur à 10";
    	}
    }
    


    ExceptionIndiceSupCoursSuivis.java
    package packExceptions;
    
    public class ExceptionIndiceSupCoursSuivis extends Exception {
    
    	private static final long serialVersionUID = 2479009251089708674L;
    	private int id;
    	
    	public ExceptionIndiceSupCoursSuivis(int id) {
    		this.id = id;
    	}
    	
    	public String getMessage() {
    		return "Le numéro entré est trop grand pour la liste suivie par l'étudiant";
    	}
    }
    
    • Partager sur Facebook
    • Partager sur Twitter
      9 novembre 2008 à 1:26:41

      c'est normal qu'il s'arrête à la première. Car tu cacth en premier les exceptions de types exception, c'est à dire toute puisque toute les exceptions hérite de la classe exception. Si tu met un catch(Exception e) il doit être en dernier, car dans ce cas, il va d'abord regarder la première exception que tu essaie d'attraper, si elle est du bon type alors il exécute les instructions, sinon il passe au catch suivant jusqu'a ce qu'il trouve le bon.

      essaye ca
      try{
      			System.out.println(e1.getCours(5) + "\n");
      			System.out.println(e1.getCours(-1) + "\n");
      			System.out.println(e1.getCours(12) + "\n");
      			System.out.println(e1.getCours(1) + "\n");
      
      		}
      		
      		catch(ExceptionIndiceNegatif e){
      			System.out.println(e.getMessage()); // Affiche la valeur incorrecte
      		}
      		catch(ExceptionIndiceSup10 e){
      			System.out.println(e.getMessage()); // Affiche la valeur incorrecte
      		}
      		catch(ExceptionIndiceSupCoursSuivis e){
      			System.out.println(e.getMessage()); // Affiche la valeur incorrecte
      		}
                      catch(Exception e){
      			System.out.println(e.getMessage()); // Affiche la valeur incorrecte
      		}
      


      • Partager sur Facebook
      • Partager sur Twitter
        9 novembre 2008 à 1:45:28

        Merci de m'aider.
        Cependant, avec ton code, il gère les 2 premières méthodes mais pas la 3 et 4ème.

        Sortie :
        Nombre de cours : 2
        
        Le numéro entré est trop grand pour la liste suivie par l'étudiant
        null
        
        Le numéro du cours est négatif
        -1
        Bouh !
        • Partager sur Facebook
        • Partager sur Twitter

        Problème de gestion de plusieurs méthodes dans try

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown