// MODULE: ExcepTest.java // AUTEUR: felipe // OBJET: montrer le mecanisme des Exceptions // COMMENT: le programmme est ici stupide, mais il illustre le mecanisme... import java.io.*; // pour les IOExceptions import java.net.*; // pour l'URL // un exemple de creation d'une nouvelle exception (completement inutile ici) // j'ai choisi ici d'etendre ArithmeticException, mais toute Exception aurait fonctionne class MyException extends ArithmeticException { public MyException() {} public MyException(String s) {super(s);} } // une classe completement idiote... // the throws MyException est ici inutile car ArithmeticException en est mere class Animation { public void chargeImage(String s, int val) throws IOException, ArithmeticException, MyException { URL url = new URL(s); int j = 4 / val; if (val == 10) { throw new MyException("une Exception stupide"); } } } // la classe principale public class ExcepTest { public static void main(String args[]) { Animation anim = new Animation(); // creation d'un onjet idiot if (args.length != 2) { System.err.println("syntaxe: ExceptTest "); System.exit(1); } // un bloc try/catch // on pourrait ici faire un traitement commun a toutes les Exceptions (un seul catch) try { anim.chargeImage(args[0],Integer.parseInt(args[1])); System.out.println("Animation ok avec " + args[0]); } catch (IOException e) { System.out.println("catch IOException de" + e); } catch (MyException e) { System.out.println("catch MyException de " + e); } catch (ArithmeticException e) { // pas propre System.out.println("catch ArithmeticException de " + e); } finally { System.out.println("finally block"); } System.out.println("apres le try bloc"); } }