Le tp2 et les exceptions • Traitement des exceptions – le bloc try/catch • révisions • NFP121- tp2 java/impératif // hypothèse : args[0] == "123"; try{ String str = args[0]; int i = Integer.parseInt(str); int résultat = unCalcul(i); System.out.println(" résultat = " + résultat ); }catch(NumberFormatException nfe){ System.out.println(" exception ! "); } ... private static int unCalcul(int x){ return x+1;} résultat = 124 // hypothèse : args[0] == "xyz"; try{ String str = args[0]; int i = Integer.parseInt(str); int résultat = unCalcul(i); System.out.println(" résultat = " + résultat ); }catch(NumberFormatException nfe){ System.out.println(" exception ! "); } ... exception ! // hypothèse : args == null; try{ String str = args[0]; int i = Integer.parseInt(str); int résultat = unCalcul(i); System.out.println(" résultat = " + résultat ); }catch(NumberFormatException nfe){ System.out.println(" exception ! "); } ... ??? try{ // « JVM » // hypothèse : args == null; try{ String str = args[0]; int i = Integer.parseInt(str); int résultat = unCalcul(i); System.out.println(" résultat = " + résultat ); }catch(NumberFormatException nfe){ System.out.println(" exception ! "); } … … }catch(Exception e){ e.printStackException(); } Par défaut la JVM « attrape » l ’exception // hypothèse : args.length==0; try{ String str = args[1]; int i = Integer.parseInt(str); int résultat = unCalcul(i); System.out.println(" résultat = " + résultat ); }catch(NumberFormatException nfe){ System.out.println(" exception ! "); } catch(ArrayIndexOutOfBoundsException e){ System.out.println(" exception ! "); ... } exception ! Passage à la pratique : le TP2, le mail d’un étudiant Bonjour, Lors du TP, nous avons obtenu une erreur dont on ne connait pas la source. Après tests avec Findbugs et PMD, toujours la même erreur … Le test de l'applet (zero absolu, mauvaise entrée) ne renvoie aucune erreur. … Submitter : 1) test_AppletteFahrenheit_convertir(question3): exception inattendue ! java.lang.NullPointerException 2) test_AppletteFahrenheit_convertir_avec_erreur(question3): exception inattendue ! java.lang.NullPointerException 3) test_AppletteFahrenheit_convertir_bis(question3): exception inattendue ! java.lang.NullPointerException 4) test_AppletteFahrenheit_ZéroAbsolu(question3): exception inattendue ! java.lang.NullPointerException Ci joint le code source. JNEWS : 17 sur 33 dans ce cas le code source un extrait. public void init(){ //…. try{ getContentPane().setBackground(Color.decode(getParameter("backgroundColor"))); } catch (NumberFormatException nfe){ getContentPane().setBackground(Color.pink); } //…. } http://java.sun.com/j2se/1.5.0/docs/api/java/applet/Applet.html#getParameter(java.lang.String) Returns:the value of the named parameter, or null if not set. Color.decode avec « une bonne valeur » Color.decode avec « une mauvaise valeur » Color.decode avec « null » Sans oublier setBackground(c ) avec une « bonne couleur » avec « null » public void init(){ //…. try{ getContentPane().setBackground(Color.decode(getParameter("backgroundColor"))); } catch (NumberFormatException nfe){ getContentPane().setBackground(Color.pink); } //…. } Color.decode avec « une mauvaise valeur » Color.decode avec « null » --> ??? --> NumberFormatException NullPointerException • String paramètre = getParameter("backgroundColor"); Pour un des tests distants avec JNEWS : backgroundColor est absent paramètre == null Submitter : 1) test_AppletteFahrenheit_convertir(question3): exception inattendue ! java.lang.NullPointerException 2) test_AppletteFahrenheit_convertir_avec_erreur(question3): exception inattendue ! java.lang.NullPointerException 3) test_AppletteFahrenheit_convertir_bis(question3): exception inattendue ! java.lang.NullPointerException 4) test_AppletteFahrenheit_ZéroAbsolu(question3): exception inattendue ! java.lang.NullPointerException public void init(){ //…. try{ getContentPane().setBackground(Color.decode(getParameter("backgroundColor"))); } catch (NumberFormatException nfe){ getContentPane().setBackground(Color.pink); } catch (NullPointerException npe){ getContentPane().setBackground(Color.pink); } //…. } Bravo !!! public void init(){ // question ? : ici quelles sont les hypothèses effectuées ? try{ String paramètre = getParameter("backgroundColor"); // question ? : domaine de paramètre, quelles sont les valeurs possibles ? // question ? : toutes ces valeurs sont-elles compatibles avec Color.decode ? Color couleurDuFond = Color.decode(paramètre); // question ? : domaine de couleurDuFond , quelles sont les valeurs possibles ? // question ? : toutes ces …etc... getContentPane() == null ??? getContentPane().setBackground(couleurDuFond); } catch (NumberFormatException nfe){ getContentPane().setBackground(Color.pink); } catch (NullPointerException npe){ getContentPane().setBackground(Color.pink); } // exceptions : la liste est-elle exhaustive ? } • En conclusion – Programmation == discipline rigoureuse – « gratifiant » de garder le contrôle … – avec « google » rules good programming –…. Writing Correct Programs(Section 9.2 ) http://www.faqs.org/docs/javap/c9/index.html Programming style: how to keep control of your programs http://www.iu.hio.no/~mark/lectures/ProgrammingStyleGuide.html