Error
),RuntimeException
),Exception
).java.lang.Object | | java.lang.Throwable | _______________|_______________ | | \_/ \_/ java.lang.Error java.lang.Exception | ____________________|____________________ | | \_/ \_/ java.lang.RuntimeException [Autres sous-classes de la classe Exception]Les exceptions vérifiées et non vérifiées par le compilateur (checked/unchecked exceptions) :
Exception
mais qui ne sont pas du type (ou sous type de) RuntimeException
.Throwable
,
de type (ou sous type de) Error
,
ou de type (ou sous type de) RuntimeException
.package com.mtitek.exceptions; public class MyTest1 { public static void main(String[] args) { System.out.println("main: start"); try { int a = 10 / 0; } catch (Exception e) { e.printStackTrace(); } System.out.println("main: end"); } }Résultat d'exécution du code :
main: start java.lang.ArithmeticException: / by zero at com.mtitek.exceptions.MyTest1.main(MyTest1.java:7) main: endNOTE : L'exception
ArithmeticException
est une sous-classe de la classe RuntimeException
(unchecked exception),
et donc le compilateur ne se plaindra pas si on ne gère pas cette exception par un bloc try/catch :package com.mtitek.exceptions; public class MyTest1 { public static void main(String[] args) { System.out.println("main: start"); int a = 10 / 0; System.out.println("main: end"); } }Résultat d'exécution du code :
main: start Exception in thread "main" java.lang.ArithmeticException: / by zero at com.mtitek.exceptions.MyTest1.main(MyTest1.java:7)Cette fois ci, lorsque la JVM a rencontré l'exception, le programme a terminé son exécution.
Throwable
,
elle n'a cependant un vrai intérêt que pour les exceptions de type (ou sous type de) Exception
.
En fait, le programme doit gérer seulement les exceptions applicatives, et rare les cas où on s'intéressera à gérer les exceptions de type Error
(erreurs JVM par exemple).try { int a = 10 / 0; } // Syntax error, insert "Finally" to complete BlockStatements
try { // ... } catch (ArithmeticException e) { // ... } catch (Exception e) { // ... }
try { // ... } catch (Exception e) { // ... } catch (Exception e) { // Unreachable catch block for Exception. It is already handled by the catch block for Exception // ... }
try { // ... } catch (Exception e) { // ... } catch (ArithmeticException e) { // Unreachable catch block for ArithmeticException. It is already handled by the catch block for Exception // ... }
public static void main(String[] args) { System.out.println("main: start"); try { // outer try try { // inner try int a = 10 / 0; // La JVM va déclencher une première exception ICI. } catch (ArithmeticException e) { System.out.println("inner try : catch ArithmeticException"); int b = 10 / 0; // La JVM va déclencher une deuxième exception ICI. } catch (Exception e) { System.out.println("inner try : catch Exception"); // Ce bloc ne sera pas exécuté ni par la première exception ni par la deuxième exception } } catch (Exception e) { System.out.println("outer try : catch Exception"); // La deuxième exception sera attrapée ICI } System.out.println("main: end"); }Résultat d'exécution du code :
main: start inner try : catch ArithmeticException outer try : catch Exception main: end
public static void main(String[] args) { System.out.println("main: start"); try { // outer try try { // inner try int a = 10 / 0; // La JVM va déclencher une exception ICI. } finally { System.out.println("inner try : finally"); } } catch (Exception e) { System.out.println("outer try : Exception"); } System.out.println("main: end"); }Résultat d'exécution du code :
main: start inner try : finally outer try : Exception main: end
public static void main(String[] args) { System.out.println("main: start"); try { // outer try try { // inner try int a = 10 / 0; // La JVM va déclencher une première exception ICI. } catch (ArithmeticException e) { System.out.println("inner try : catch ArithmeticException"); int b = 10 / 0; // La JVM va déclencher une deuxième exception ICI. } finally { System.out.println("inner try : finally"); } } catch (Exception e) { System.out.println("outer try : Exception"); } System.out.println("main: end"); }Résultat d'exécution du code :
main: start inner try : catch ArithmeticException inner try : finally outer try : Exception main: end
package com.mtitek.exceptions; public class MyTest1 { public static void main(String[] args) { System.out.println("main: start"); try { int v1 = 10; int v2 = 0; if(v2 == 0) { throw new ArithmeticException("Attention, division entière / 0 !"); } else { int v = v1 / v2; } } catch (ArithmeticException e) { e.printStackTrace(); } System.out.println("main: end"); } }Résultat d'exécution du code :
main: start java.lang.ArithmeticException: Attention, division entière / 0 ! at com.mtitek.exceptions.MyTest1.main(MyTest1.java:12) main: endCet exemple est inutile, mais on y voit bien l'utilisation du mot clé throw pour lancer une exception applicative.
package com.mtitek.exceptions; public class MyTest1 { public static void main(String[] args) { System.out.println("main: start"); try { int v1 = divide(10, 0); } catch (Exception e) { e.printStackTrace(); } System.out.println("main: end"); } private static int divide(int p1, int p2) throws Exception { // déclaration de l'exception if(p2 == 0) { throw new Exception("Attention, division entière / 0 !"); } else { return p1 / p2; } } }Résultat d'exécution du code :
main: start java.lang.Exception: Attention, division entière / 0 ! at com.mtitek.exceptions.MyTest1.divide(MyTest1.java:18) at com.mtitek.exceptions.MyTest1.main(MyTest1.java:8) main: endUne méthode doit soit gérer ou déclarer les exceptions déclarées par les méthodes appelées par le code de cette méthode (cella s'applique uniquement aux exceptions vérifiables par le compilateur).
package com.mtitek.exceptions; public class MyTest1 { public static void main(String[] args) throws Exception { System.out.println("main: start"); int v1 = divide(10, 0); System.out.println("main: end"); } private static int divide(int p1, int p2) throws Exception { if(p2 == 0) { throw new Exception("Attention, division entière / 0 !"); } else { return p1 / p2; } } }Résultat d'exécution du code :
main: start Exception in thread "main" java.lang.Exception: Attention, division entière / 0 ! at com.mtitek.exceptions.MyTest1.divide(MyTest1.java:14) at com.mtitek.exceptions.MyTest1.main(MyTest1.java:7)Notez que le code n'a pas terminé son exécution à cause que l'exception n'a pas été gérer par un bloc try catch et comme la méthode main et la dernière méthode de la pile d'exécution, la JVM arrête donc la propagation de l'exception et met fin à l'exécution du programme.
public class BigNumberException extends Exception { public BigNumberException() { super("Big Number Exception"); } }
public class SmallNumberException extends Exception { public SmallNumberException() { super("Small Number Exception"); } }
package com.mtitek.exceptions; public class MyTest1 { public static void main(String[] args) throws SmallNumberException { System.out.println("main: start"); try { int v1 = divide(10, 0); } catch (BigNumberException e) { e.printStackTrace(); } catch (SmallNumberException e) { e.printStackTrace(); } catch (ArithmeticException e) { e.printStackTrace(); } try { int v1 = divide(100, 2); } catch (BigNumberException e) { e.printStackTrace(); } catch (SmallNumberException e) { e.printStackTrace(); } catch (ArithmeticException e) { e.printStackTrace(); } try { int v1 = divide(-10, 2); } catch (BigNumberException e) { e.printStackTrace(); } // Dans le cas de cet exemple, l'exécution du programme va s'arrêter avant d'atteindre la fin du code. System.out.println("main: end"); } private static int divide(int p1, int p2) throws SmallNumberException, BigNumberException { if(p2 == 0) { throw new ArithmeticException("Attention, division entière / 0 !"); // Pas besoin de déclarer cette exception car non vérifiées par le compilateur } else if(p1 < 0) { throw new SmallNumberException(); } else if(p1 > 10) { throw new BigNumberException(); } else { return p1 / p2; } } }Résultat d'exécution du code :
main: start java.lang.ArithmeticException: Attention, division entière / 0 ! at com.mtitek.exceptions.MyTest1.divide(MyTest1.java:43) at com.mtitek.exceptions.MyTest1.main(MyTest1.java:8) com.mtitek.exceptions.BigNumberException: Big Number Exception at com.mtitek.exceptions.MyTest1.divide(MyTest1.java:47) at com.mtitek.exceptions.MyTest1.main(MyTest1.java:20) Exception in thread "main" com.mtitek.exceptions.SmallNumberException: Small Number Exception at com.mtitek.exceptions.MyTest1.divide(MyTest1.java:45) at com.mtitek.exceptions.MyTest1.main(MyTest1.java:32)