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
‣Exceptions
‣Lancement (throw)
‣Capture (try-catch)
‣Propagation (throws)
Sumary
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
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
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
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)
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
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
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 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
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