• Aucun résultat trouvé

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

N/A
N/A
Protected

Academic year: 2022

Partager "UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD10 - Exceptions"

Copied!
13
0
0

Texte intégral

(1)

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/

(2)

2

‣Exceptions

‣Lancement (throw)

‣Capture (try-catch)

‣Propagation (throws)

Sumary

(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)

(4)

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

(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

(6)

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

(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

(8)

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)

(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

(10)

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

(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("")){

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

Propagation des exceptions : Throws

(12)

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");

} } }

Finally

java TestException2

Error::checkStr::String is empty Valeur default

(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);

} }

Finally

$java TestException2 abc

Références

Documents relatifs

On ne peut pas utiliser l’opérateur « point » (.) sur les variables de la classe Chien ailleurs que dans la classe Chien

UE 2I002 (ex LI230) : éléments de programmation par objets avec Java!. TD4 - Composition,

‣ Un tableau est une structure de données contenant un groupe d'éléments tous du même type. ‣ Le type des éléments peut être un type primitif ou

‣ boolean, byte, short, char, int, long, float et double, sont associées des classes Boolean, Byte, Short, Character, Integer, Long, Float et Double. ‣ Ces classes ont des

6OF EFT QSPQSJÏUÏT JOEVJUFT QBS MF QPMZNPSQIJTNF FTU RVF MJOUFSQSÏUFVS +BWB FTU DBQBCMF EF USPVWFS MF USBJUFNFOU Ë FČFDUVFS MPST EF MBQQFM EVOF NÏUIPEF TVS VO PCKFU "JOTJ

-F DPODFQU EF DMBTTF BCTUSBJUF TF TJUVF FOUSF DFMVJ EF DMBTTF FU DFMVJ EJOUFSGBDF $FTU VOF DMBTTF RVPO OF QFVU QBT EJSFDUFNFOU JOTUBODJFS DBS DFSUBJOFT EF TFT NÏUIPEFT OF TPOU

‣ On peut affecter à un champ ou une variable d'un type une expression de type moins élevé dans la hiérarchie des types. int a

Depuis le début de l’année, nous utilisons systématiquement la méthode main avec un paramètre de type String[], c’est à dire un tableau de chaînes de caractères. Ce