ue 2i002 (ex li230) : éléments de programmation par objets ... · ue 2i002 (ex li230) :...
TRANSCRIPT
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/
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
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
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
$ java TestException 10 0 Error / by zero
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
$ java TestException 10 0i Error For input string: "0i"
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("")){ 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
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
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