IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

avec Java Discussion :

Les Exceptions ,Try Catch


Sujet :

avec Java

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2008
    Messages : 22
    Points : 11
    Points
    11
    Par défaut Les Exceptions ,Try Catch
    Voici un code ou je gère les exceptions dans le cas d'une saisit d'un indice qui n'est pas compris dans les bornes du tableau,en cas de divison par zéro.

    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
     
    class exo11_1 {
        static int [] tableau={17,12,15,38,29,157,89,-22,0,5 };
     
        static int division(int indice,int diviseur) {
            return tableau[indice] / diviseur ;
     
            } 
     
        public static void main(String[] args ) {
            int x,y;
     
            do {
           try {
            Terminal.ecrireStringln("Entrez l'indice de l'entier a diviser ");
            x=Terminal.lireInt();
            if(x<0 || x > tableau.length ) {
                throw new ArrayIndexOutOfBoundsException();
            } 
           }catch(ArrayIndexOutOfBoundsException e) {
               Terminal.ecrireStringln("Entrez l'indice de l'entier a diviser ");
               x=Terminal.lireInt();
           } 
     
     
            } while(x < 0 || x > tableau.length);
     
     
           do {
                try {
            Terminal.ecrireString("entrez le diviseur ");
            y=Terminal.lireInt();  
            if(y==0) {
                throw new ArithmeticException();
            }
                } catch (ArithmeticException a ) {
                    Terminal.ecrireStringln("entrez le diviseur");
                    y=Terminal.lireInt();
                } 
           } while(y == 0);
            Terminal.ecrireStringln("le resultat de la division est ");
            Terminal.ecrireIntln(division(x,y));
        }
     
     
     
        }
    Ce que je n'arrive pas a géré c'est dans le cas ou l'utilisateur entre autre chose qu'un entier, genre un "char " par exemple.
    Dans mon if(Comment exprimé en java si la saisit n'est pas entier alors je leve l'exception ...
    Merci

  2. #2
    Expert éminent
    Avatar de djo.mos
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    4 666
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 4 666
    Points : 7 679
    Points
    7 679
    Par défaut
    Bonjour,
    Ca dépend pas de toi car tu utilises une classe "artisanale" Terminal ... Fais un test et regardes est ce que lireInt lance une exception quand on ne saisit pas de int ...

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2008
    Messages : 22
    Points : 11
    Points
    11
    Par défaut Suite
    Voila ce qui se passe a la compile lorsque la saisit est differente d'un entier

    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
     
     
    nit:
    deps-jar:
    Compiling 1 source file to C:\Users\ginolastar\Terminal.java\build\classes
    compile-single:
    run-single:
    saisir l'indice de  l'entier a diviser 
    "
    Exception in thread "main" TerminalException
            at Terminal.exceptionHandler(Terminal.java:117)
            at Terminal.lireInt(Terminal.java:25)
            at Exo11.main(Exo11.java:12)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 3 seconds)
    Merci en tout cas d'avoir répondu

  4. #4
    Expert éminent
    Avatar de djo.mos
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    4 666
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 4 666
    Points : 7 679
    Points
    7 679
    Par défaut
    Je n'en suis pas sûr mais apparamment, lirInt se débrouille toute seule en catchant l'exception et en quittant ....
    Juste pour tester, ajoutes un system.out.print apprès lireInt et refais le test.

  5. #5
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2008
    Messages : 22
    Points : 11
    Points
    11
    Par défaut suite
    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
    class exo11_1 {
        static int [] tableau={17,12,15,38,29,157,89,-22,0,5 };
        
        static int division(int indice,int diviseur) {
            return tableau[indice] / diviseur ;
                
            } 
        
        public static void main(String[] args ) {
            int x,y;
            
            do {
           try {
            Terminal.ecrireStringln("Entrez l'indice de l'entier a diviser ");
            x=Terminal.lireInt();
            if(x<0 || x > tableau.length ) {
                throw new ArrayIndexOutOfBoundsException();
            } 
           }catch(ArrayIndexOutOfBoundsException e) {
               Terminal.ecrireStringln("Entrez l'indice de l'entier a diviser ");
               x=Terminal.lireInt();
               System.out.print(x); 
           }
    Ca ne la rattrappe pas apprement
    init:
    deps-jar:
    Compiling 1 source file to C:\Users\ginolastar\Terminal.java\build\classes
    compile-single:
    run-single:
    saisir l'indice de l'entier a diviser
    é
    Exception in thread "main" TerminalException
    at Terminal.exceptionHandler(Terminal.java:117)
    at Terminal.lireInt(Terminal.java:25)
    at Exo11.main(Exo11.java:12)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 6 seconds)
    Merci

  6. #6
    Expert éminent
    Avatar de djo.mos
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    4 666
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 4 666
    Points : 7 679
    Points
    7 679
    Par défaut
    Non, je voulais dire mettre le println juste après le lireInt, pas dans le bloc catch ...

  7. #7
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2008
    Messages : 22
    Points : 11
    Points
    11
    Par défaut suite
    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
     
    class exo11_1 {
        static int [] tableau={17,12,15,38,29,157,89,-22,0,5 };
     
        static int division(int indice,int diviseur) {
            return tableau[indice] / diviseur ;
     
            } 
     
        public static void main(String[] args ) {
            int x,y;
     
            do {
           try {
            Terminal.ecrireStringln("Entrez l'indice de l'entier a diviser ");
            x=Terminal.lireInt();
            if(x<0 || x > tableau.length ) {
                throw new ArrayIndexOutOfBoundsException();
            } 
           }catch(ArrayIndexOutOfBoundsException e) {
               Terminal.ecrireStringln("Entrez l'indice de l'entier a diviser ");
               x=Terminal.lireInt();
     
           }
            System.out.println(x); // en dehors du catch
     
            } while(x < 0 || x > tableau.length);
     
     
           do {
                try {
            Terminal.ecrireString("entrez le diviseur ");
            y=Terminal.lireInt();  
            if(y==0) {
                throw new ArithmeticException();
            }
                } catch (ArithmeticException a ) {
                    Terminal.ecrireStringln("entrez le diviseur");
                    y=Terminal.lireInt();
                } 
           } while(y == 0);
            Terminal.ecrireStringln("le resultat de la division est ");
            Terminal.ecrireIntln(division(x,y));
        }
     
     
     
        }
    A la compile

    init:
    deps-jar:
    Compiling 1 source file to C:\Users\ginolastar\Terminal.java\build\classes
    compile-single:
    run-single:
    saisir l'indice de l'entier a diviser
    é
    Exception in thread "main" TerminalException
    at Terminal.exceptionHandler(Terminal.java:117)
    at Terminal.lireInt(Terminal.java:25)
    at Exo11.main(Exo11.java:12)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 4 seconds)

  8. #8
    Expert éminent
    Avatar de djo.mos
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    4 666
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 4 666
    Points : 7 679
    Points
    7 679
    Par défaut
    Oki, essaies de remplacer ton catch actuel par:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    catch(Exception e) {
    ....
    }

  9. #9
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2008
    Messages : 22
    Points : 11
    Points
    11
    Par défaut suite try catch
    Donc voici un code qui gere 3 exeptions

    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
     
    public class menu {
        public static int renvoie_entier(int entier ) throws inf_ou_egal,exclus {
            if(entier <= 1) throw new inf_ou_egal();
     
            Terminal.ecrireStringln("Entrez une valeur entre 1 et n ");
            int compris_entre=Terminal.lireInt(); 
     
            if(compris_entre < 1 || compris_entre > entier)  throw new exclus();
     
            return compris_entre;
        } 
        public static void main(String[]args ) {
            boolean ok=true;
            int val; 
            do {
            try {
     
              Terminal.ecrireStringln("saisissez un entier ");
              val=Terminal.lireInt();  
               int rep=renvoie_entier(val);
        } catch(inf_ou_egal e ) {
            Terminal.ecrireStringln("entier saisit inferieur ou egal a 1");
            ok=false;
        } catch(exclus a) {
            Terminal.ecrireStringln("l'entier renvoyé n'est pas compris entre 1 et n ");
            ok=false;
        } catch (TerminalException g) {
            Terminal.ecrireStringln("n'est pas un entier ");
            ok=false;
        }
    } while(! ok); 
            Terminal.ecrireStringln("c'est fini"); // pour verifie si en cas de bonne saisit utilisateur je suis bien hors du do while
        }
    }
    class inf_ou_egal extends Exception{}  
    class exclus extends Exception {}
    Comment se fait-il en cas de bonne saisit utilisateur je ne sors jamais de mon do while
    Merci
    class exclus extends Exception {}

  10. #10
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    Il faudrais que tu remette ton boolean ok à true en début de boucle do-while.

Discussions similaires

  1. Optimiser les performances try/catch ?
    Par KiLVaiDeN dans le forum Langage
    Réponses: 4
    Dernier message: 14/01/2014, 13h47
  2. Avis de développeur sur mon code - Exception try/catch/finally
    Par psykoprof dans le forum Débuter avec Java
    Réponses: 15
    Dernier message: 02/06/2010, 00h01
  3. ID Exception Try Catch
    Par zentro dans le forum Général Dotnet
    Réponses: 0
    Dernier message: 02/12/2009, 15h00
  4. [Système] Exception: try / catch
    Par canabral dans le forum Langage
    Réponses: 2
    Dernier message: 05/09/2006, 12h28
  5. Exception & Try..catch
    Par PurL dans le forum C++Builder
    Réponses: 2
    Dernier message: 11/12/2002, 15h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo