Presentation is loading. Please wait.

Presentation is loading. Please wait.

Animation du tp2 Traitement des exceptions Le bloc try/catch/finally

Similar presentations


Presentation on theme: "Animation du tp2 Traitement des exceptions Le bloc try/catch/finally"— Presentation transcript:

1 Animation du tp2 Traitement des exceptions Le bloc try/catch/finally
NFP ème année de licence

2 // 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;}

3 // 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 ! "); } ...

4 // 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 ! "); } ... ???

5 // hypothèse : args == null; try{ String str = args[0];
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

6 // 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(ArrayIndexOutOfBoundsException e){ System.out.println(" exception ! "); }

7 Ci joint le code source. JEWS : 21 sur 33 dans ce cas
Passage à la pratique : le TP2 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 et l'intervenant n'a pas trouvé non plus ce qui pourrait etre l'explication. Le test de l'applet (zeor 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 ! Ci joint le code source. JEWS : 21 sur 33 dans ce cas

8 public void init(){ le code source un extrait. Returns:
//…. try{ getContentPane().setBackground(Color.decode(getParameter("backgroundColor"))); } catch (NumberFormatException nfe){ getContentPane().setBackground(Color.pink); 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 »

9 public void init(){ NumberFormatException ??? NullPointerException
//…. 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

10 Tests distants avec JEWS : backgroundColor est absent
String paramètre = getParameter("backgroundColor"); Tests distants avec JEWS :  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 !

11 catch (NullPointerException npe){
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 !!!

12 // exceptions : la liste est-elle exhaustive ?
public void init(){ //…. try{ String paramètre = getParameter("backgroundColor"); Color couleurDuFond = Color.decode(paramètre); getContentPane().setBackground(couleurDuFond); } catch (NumberFormatException nfe){ getContentPane().setBackground(Color.pink); } catch (NullPointerException npe){ } // question ? : domaine de paramètre, quelles sont les valeurs possibles ? // question ? : toutes ces valeurs sont-elles compatibles avec Color.decode ? // question ? : domaine de couleurDuFond , quelles sont les valeurs possibles ? // question ? : etc... // exceptions : la liste est-elle exhaustive ?

13 En conclusion Programmation == discipline rigoureuse
recherche « google » rules good programming Writing Correct Programs(Section 9.2 ) Programming style: how to keep control of your programs


Download ppt "Animation du tp2 Traitement des exceptions Le bloc try/catch/finally"

Similar presentations


Ads by Google