UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) :...

13
UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes [email protected] http://www.lcqb.upmc.fr/julianab/teaching/JAVA/

Transcript of UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) :...

Page 1: UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes

UE 2I002 (ex LI230) : éléments de programmation par objets avec Java

TD10 - Exceptions

Juliana Silva [email protected]

http://www.lcqb.upmc.fr/julianab/teaching/JAVA/

Page 2: UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes

2

‣Exceptions

‣Lancement (throw)

‣Capture (try-catch)

‣Propagation (throws)

Sumary

Page 3: UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes

3

Exceptions

‣ Les exceptions représentent le mécanisme de gestion des erreurs

public class TestException { public static void main(String[] args){ int i = 3; int j = 0; System.out.println("résultat = " + (i / j));

} }

$java TestException Exception in thread "main" java.lang.ArithmeticException: / by zero at tests.TestException.main(TestException.java:5)

Page 4: UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes

4

Exceptions

public class TestException { public static void main(String[] args) { //Insert code to start the application here. int i = 3; int j = 0;

try { System.out.println("résultat = " + (i / j));

} catch (ArithmeticException e) {

System.out.println("Error"); }

}

‣ Les bloc try catch

Page 5: UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes

5

Exceptions

public class TestException { public static void main(String[] args) { //Insert code to start the application here. int i = 3; int j = 0;

try { System.out.println("résultat = " + (i / j));

} catch (ArithmeticException e) {

System.out.println("Error" + e.getMessage()); }

}

‣ Les bloc try catch

$ java TestException Error / by zero

Page 6: UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes

6

Exceptions

➡Les classes Exception, RunTimeException et Error‣ Ces trois classes descendent de Throwable : toutes les exceptions

dérivent de la classe Throwable.

‣ La classe Error représente une erreur grave intervenue dans la machine virtuelle Java

Page 7: UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes

7

Exceptions

public class TestException { public static void main(String[] args) { //Insert code to start the application here.

int i; int j; try { i = Integer.parseInt(args[0]); j = Integer.parseInt(args[1]); System.out.println("resultat = " + (i/j)); } catch (ArithmeticException e) { System.out.println("Error " + e.getMessage()); } }

‣ Les bloc try catch

$ java TestException 10 2 resultat = 5

$ java TestException 10 0 Error / by zero

Page 8: UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes

8

Exceptions

public class TestException { public static void main(String[] args) { //Insert code to start the application here.

int i; int j; try { i = Integer.parseInt(args[0]); j = Integer.parseInt(args[1]); System.out.println("resultat = " + (i/j)); } catch (ArithmeticException e) { System.out.println("Error " + e.getMessage()); } }

‣ Les bloc try catch

$ java TestException 10 0q

Exception in thread "main" java.lang.NumberFormatException: For input string: "0i" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at TestException.main(TestException.java:8)

Page 9: UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes

9

Exceptions

public class TestException { public static void main(String[] args) { //Insert code to start the application here.

int i; int j; try { i = Integer.parseInt(args[0]); j = Integer.parseInt(args[1]); System.out.println("resultat = " + (i/j)); } catch (ArithmeticException e) { System.out.println("Error " + e.getMessage()); }

catch (NumberFormatException e) { System.out.println("Error " + e.getMessage()); } }

‣ Les bloc try catch

$ java TestException 10 0i Error For input string: "0i"

Page 10: UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes

10

Throw public class MyException extends Exception { public MyException() { super(); } public MyException(String s) { super(s); } }

public class TestException2 { public static void main(String[] args) { //Insert code to start the application here. String strEmpty = "";

try { if (strEmpty.equals("")){ throw new MyException("String is empty"); } } catch (MyException e) { System.out.println("Error " + e.getMessage()); } } }

throw

Page 11: UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes

11

public class MyException extends Exception { public MyException() { super(); } public MyException(String s) { super(s); } }

public class TestException2 { public static void main(String[] args) { //Insert code to start the application here. String strEmpty = ""; try { checkStr(strEmpty); } catch (MyException e) { System.out.println("Error " + e.getMessage()); } }

public static void checkStr(String str) throws MyException{ if (strEmpty.equals("")){ throw new MyException("::checkStr::String is empty"); } } }

il n’y a pas de bloc try …catch il faut rapporter l'exception

Propagation des exceptions : Throws

Page 12: UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes

12

public class MyException extends Exception { public MyException() { super(); } public MyException(String s) { super(s); } }

public class TestException2 { public static void main(String[] args) { //Insert code to start the application here. String strEmpty = ""; try { checkStr(strEmpty); } catch (MyException e) { System.out.println("Error " + e.getMessage());

strEmpty = "Valeur default"; } finally{ System.out.println(strEmpty); }

}

public static void checkStr(String str) throws MyException{ if (strEmpty.equals("")){ throw new MyException("::checkStr::String is empty"); } } }

Finallyjava TestException2 Error::checkStr::String is empty Valeur default

Page 13: UE 2I002 (ex LI230) : éléments de programmation par objets ... · UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions Juliana Silva Bernardes

13

public class MyException extends Exception { public MyException() { super(); } public MyException(String s) { super(s); } }

public class TestException2 { public static void main(String[] args) { //Insert code to start the application here. String strEmpty = "abc"; try { checkStr(strEmpty); } catch (MyException e) { System.out.println("Error " + e.getMessage());

strEmpty = "Valeur default"; } finally{ System.out.println(strEmpty); }

}

public static void checkStr(String str) throws MyException{ if (strEmpty.equals("")){ throw new MyException("::checkStr::String is empty"); } } }

Finally$java TestException2 abc