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

Java Discussion :

[Exception]Bonne gestion ?


Sujet :

Java

  1. #1
    Membre confirmé
    Inscrit en
    Février 2005
    Messages
    157
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 157
    Par défaut [Exception]Bonne gestion ?
    Bonjour,

    Je suis débutante dans la gestion des exceptions et je rencontre un problème dans mon main.

    J'ai une méthode connection qui envoie une erreur qui pointe sur mon fichiers ClientLDAPException.java.

    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
     
    public void connection (String url, String dn, String psw) throws ch.ldap.ClientLDAPException {
        Hashtable<String,String> env;
        env = new Hashtable<String,String>();
        env.put (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put (Context.PROVIDER_URL, url);
        env.put (Context.SECURITY_AUTHENTICATION, "simple");
        env.put (Context.SECURITY_PRINCIPAL, dn);
        env.put (Context.SECURITY_CREDENTIALS, psw);
     
        try {
          ctx = new InitialDirContext(env);
        } catch ( javax.naming.NamingException e ) {
    	throw new ch.esnig.ldap.ClientLDAPException("Error : Initialisation du context LDAP! (connection)", e );
        }
      }
    Ensuite, dans mon main, si je saisis un uid erroné, il m'affiche une liste d'erreurs que j'aimerais récupérer pour afficher un message plus compréhensible.

    Comme il semble dire que l'erreur est de type "javax.naming.InitialContext", je la "catche", mais ça ne doit pas être la bonne technique.

    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
     
    public static void main(String[] args) throws ch.ldap.ClientLDAPException {
        String dn = "uid=bidon,ou=personne,dc=ecole,dc=ch";
        String psw = "xxx";
        String url = "ldap://localhost:389";
     
        ClientLDAP client = new ClientLDAP();
     
        try {
          client.connection (url, dn, psw);
      }
      catch (javax.naming.InitialContext e) {
       // System.out.println ("Probleme du credential");
       throw new ch.esnig.ldap.ClientLDAPException("Reported error : Credential! (main)", e );
      }
     
      //client.printAttributes(dn);
      //client.disconnection();
     
     
      }
    }
    Voici les 2 erreurs rencontrées :

    • cannot find symbol symbol : constructor ClientLDAPException(java.lang.String,javax.naming.InitialContext)
      location: class ch.esnig.ldap.ClientLDAPException

      throw new ch.ldap.ClientLDAPException("Reported error : Credential! (ma
      in)", e );



    Comme je ne suis pas trop expérimentée dans la gestion des erreurs, je ne comprends ce que ça veut dire.

    Est-ce qu'une bonne âme pourrait m'aider ?

    Merci d'avance.

  2. #2
    Membre Expert
    Avatar de zekey
    Profil pro
    Inscrit en
    Février 2005
    Messages
    1 036
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 1 036
    Par défaut
    Je ne comprend pas pourquoi tu lève une exception de type:
    ch.ldap.ClientLDAPException dans la méthode connection.
    Et pourquoi tu catche une javax.naming.InitialContext (c'est pas une exception ce truc non ?) dans le main.


    Le compilateur te dis simplement qu'il cherche un constructeur dans la classe
    ClientLDAPException avec comme paramètre une chaine et un context

  3. #3
    Membre confirmé
    Inscrit en
    Février 2005
    Messages
    157
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 157
    Par défaut
    Même si je catche javax.naming.AuthenticationException, j'ai l'erreur suivante :

    • exception javax.naming.AuthenticationException is never thrown in body of corresponding try statement
      catch ( javax.naming.AuthenticationException e ) {

  4. #4
    Membre chevronné
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    509
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Avril 2003
    Messages : 509
    Par défaut
    En fait il t'explique que tu cacth une exception qui n'est jamais lancer , tu doit catcher l'exception que ta methode connection est susceptible de lancer (ClientLDAPException)

  5. #5
    Membre confirmé
    Inscrit en
    Février 2005
    Messages
    157
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 157
    Par défaut
    Mon fichier ClientLDAPException ressemble à ça.... je ne vois pas trop ce que je dois changer ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    import ch.util.EsnigException;
     
    public class ClientLDAPException extends EsnigException {
      public ClientLDAPException (String message) {
        super ("Exception rencontree : " + message);
      }
     
      public ClientLDAPException (String message, Throwable cause) {
        super ("Exception rencontree : " + message, cause);
      }
    }

  6. #6
    Membre chevronné
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    509
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Avril 2003
    Messages : 509
    Par défaut
    Si ton main est toujours celui la :
    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
     
    public static void main(String[] args) throws ch.ldap.ClientLDAPException {
        String dn = "uid=bidon,ou=personne,dc=ecole,dc=ch";
        String psw = "xxx";
        String url = "ldap://localhost:389";
     
        ClientLDAP client = new ClientLDAP();
     
        try {
          client.connection (url, dn, psw);
      }
      catch (javax.naming.InitialContext e) {
       // System.out.println ("Probleme du credential");
       throw new ch.esnig.ldap.ClientLDAPException("Reported error : Credential! (main)", e );
      }
     
      //client.printAttributes(dn);
      //client.disconnection();
     
     
      }
    }
    Ca ne peut pas marcher car lors de ton throw tu fait une new ClientLDAPException(String,InitialContext) or InitialContext n'est pas une exception et pas un throwable non plus du coup il peut pas construire ton excpetion .
    Tu devrais catcher une vrai exception et changer le catch de ton main en y mettant une VRAI exception

  7. #7
    Membre confirmé
    Inscrit en
    Février 2005
    Messages
    157
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 157
    Par défaut
    J'ai changé mon main et je catche javax.naming.AuthenticationException :

    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
     
    public static void main(String[] args) throws ch.ldap.ClientLDAPException { 
        String dn = "uid=bidon,ou=personne,dc=ecole,dc=ch"; 
        String psw = "xxx"; 
        String url = "ldap://localhost:389"; 
     
        ClientLDAP client = new ClientLDAP(); 
     
        try { 
          client.connection (url, dn, psw); 
      } 
      catch (javax.naming.AuthenticationException) { 
         throw new ch.esnig.ldap.ClientLDAPException("Reported error : Credential! (main)", e ); 
      } 
       } 
    }
    mais j'ai une erreur :
    • exception javax.naming.AuthenticationException is never thrown in body of corresponding try statement
      catch ( javax.naming.AuthenticationException e ) {
      ^


    :

  8. #8
    Membre chevronné
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    509
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Avril 2003
    Messages : 509
    Par défaut
    Si tu regarde ici tu constatera que AuthentificationException est une NamingException or tu catch deja dans ta methode exception une NamingException donc si une AuthentificationException est lancé elle sera cacther dans la methode connection et une ClientLDAPException sera lancé , c'est donc cette excpetion que tu dois catcher dans ton main !!!

    Tu peut aussi catcher une Exception de cette façon quelque soit le type de probleme tu la catchera mais le message sera celui d'une CleintLDAP Exception ce qui n'est pas forcmeent coherent .

    Par contre evite de faire un throw dans le main , il n'y a rien apres le main donc personne pour catcher ton excpetion ,d'ailleur le compilateur ne le permettra pas . donc dans le catch de ton main fait :

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

  9. #9
    Membre confirmé
    Inscrit en
    Février 2005
    Messages
    157
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 157
    Par défaut
    Merci pour toutes ces informations utiles.

    Par contre, comme tu dis, en utilisant e.printStackTrace(); le message n'est pas cohérent pour un utilisateur.

    Est-ce qu'il y a une possibilité pour afficher un message compréhensible ?

    Merci beaucoup d'avance...

  10. #10
    Membre expérimenté Avatar de xxaragornxx
    Profil pro
    Inscrit en
    Mars 2003
    Messages
    241
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2003
    Messages : 241
    Par défaut
    Citation Envoyé par sangei
    Merci pour toutes ces informations utiles.

    Par contre, comme tu dis, en utilisant e.printStackTrace(); le message n'est pas cohérent pour un utilisateur.

    Est-ce qu'il y a une possibilité pour afficher un message compréhensible ?

    Merci beaucoup d'avance...
    Un getMessage() est bcp moins verbeux, mais parfois il va juste te retourner par exemple "null" pour un "NullPointerException", ce qui n'est pas vraiment parlant (justement)...
    Le top c'est écrire toi même les messages !

  11. #11
    Membre confirmé
    Inscrit en
    Février 2005
    Messages
    157
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 157
    Par défaut
    Merci beaucoup à tous pour toutes ces informations!!!


+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 0
    Dernier message: 10/09/2009, 17h48
  2. Bonne gestion des fichiers temporaires ?
    Par monstroplante dans le forum C#
    Réponses: 17
    Dernier message: 18/04/2008, 11h22
  3. Quel SGBD a une bonne gestion des LOGs ?
    Par joker vb dans le forum Décisions SGBD
    Réponses: 12
    Dernier message: 03/04/2008, 17h17
  4. [VB.NET]La bonne gestion des forms
    Par Wintermute dans le forum Windows Forms
    Réponses: 11
    Dernier message: 13/01/2004, 16h35

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