Prinzip des Exception Handling geschützter Block p(); catch (Exception e) { System.out.println(e.toString()); void p() throws Exception { throw new Exception(); Auslösen einer Exception Exception Handler Passender Exception-Handler wird in allen aktiven Methoden gesucht Exceptions sind Objekte Felder: Informationen über den Fehler Methoden: Ausgabe der Fehlerinformationen, etc. 1
Exception-Klassen Basisklasse aller Exceptions Object Systemfehler (z.b. beim Laden einer Klasse) Throwable Throwable(msg) getmessage(): String tostring(): String printstacktrace() Error RuntimeException Exception benutzerdefinierte Exceptions Checked Exceptions Laufzeitfehler (z.b. Index-Check, null-check, ) catch (MyException e) { // fängt MyException und Unterklassen catch (Exception e) { // fängt Exception und Unterklassen catch (Throwable e) { // fängt alle Exceptions 2
Checked Exceptions Exceptions, die abgefangen oder weiterleitet werden müssen Abfangen einer Exception throw new MyException(); catch (MyException e) { Weiterleiten einer Exception void foo() throws MyException { throw new MyException(); An jeder Aufrufstelle von foo() prüft der Compiler, ob MyException abgefangen oder weiterleitet wird. void bar() { foo(); catch (MyException e) { void bar() throws MyException { foo(); 3
RuntimeException Können, aber müssen nicht abgefangen werden NullPointerException Zugriff über null-referenz ArithmeticException Division durch 0, IndexOutOfBoundsException Arrayindex nicht im gültige Bereich ClassCastException Typumwandlung auf unerlaubten Typ Wenn nicht abgefangen Timer t; t.add(1); // liefert NullPointerException und bricht das Programm ab Wenn abgefangen t.add(1); catch (NullPointerException e) { System.out.println("t does not reference an object"); 4
Benutzerdefinierte Exceptions Beispiel: Exception-Klassen für Stack-Überlauf und -Unterlauf Exception StackException OverflowException UnderflowException public class StackException extends Exception { public class OverflowException extends StackException { public Object element; public OverflowException (Object e) { element = e; public class UnderflowException extends StackException { 5
Klasse Stack mit Exceptions public class Stack { private static final int MAX = 100; private Object[] data = new Object[MAX]; private int top = 0; public void push (Object obj) throws OverflowException { if (top == MAX) throw new OverflowException(obj); data[top] = obj; top++; public Object pop () throws UnderflowException { if (top == 0) throw new UnderflowException(); top--; return data[top]; Benutzung stack.push("an object"); System.out.println(stack.pop()); catch (OverflowException e) { System.out.println("stack overflow when pushing " + e.element); catch (UnderflowException e) { System.out.println("stack underflow"); 6
Multi-Catch Mehrere catches können auf ein Multi-Catch zusammengefasst werden stack.push("an object"); System.out.println(stack.pop()); catch (OverflowException UnderflowException e) { e.printstacktrace(); Statische Typ von e = StackException als gemeinsame Oberklasse von OverflowException und UnderflowException 7
Enumerationstypen Java 1.4: Menge benannter Konstantenwerte // colors static final int RED = 0; static final int BLUE = 1; static final int GREEN = 2; int color = BLUE; // priorities static final int LOW = 0; static final int NORMAL = 1; static final int HIGH = 2; int priority = HIGH; Problem: keine Typprüfung zwischen Konstantenmengen int color = HIGH; Compiler meldet keinen Fehler Java 5.0: Enumerationstypen enum Color { RED, BLUE, GREEN Color color = Color.BLUE; Compiler prüft, dass nur Color-Werte zugewiesen werden enum Priority { LOW(1), NORMAL(2), HIGH(4); private int val; private Priority(int val) { this.val = val; public int value() { return val; Priority prio = Priority.HIGH; System.out.println(prio.value()); // 4 8
Neue for-schleife Java 1.4: Iterieren über Arrays und Collections int[] primes = {2, 3, 5, 7, 11; for (int i = 0; i < primes.length; i++) { System.out.println(primes[i]); ArrayList names = new ArrayList(); names.add("alice"); names.add("bob"); Iterator iter = names.iterator(); while (iter.hasnext()) { System.out.println((String)iter.next()); Java 5.0: Neue for-schleife int[] primes = {2, 3, 5, 7, 11; for (int x: primes) { System.out.println(x); sprich: "for each int x in primes" ArrayList names = new ArrayList(); names.add("alice"); names.add("bob"); for (Object s: names) { System.out.println((String)s); 9
Variable Anzahl von Parametern Java 1.4: Methoden mit fixer Parameteranzahl static int sum(int x, int y) { return x + y; int x = sum(17, 4); kann nur die Summe von 2 Zahlen bilden Java 1.4: Methoden mit variabel vielen Parametern vom gleichen Typ static int sum(int[] val) { int res = 0; for (int i = 0; i < val.length; i++) res += val[i]; return res; int x = sum(new int[] {1, 2, 3, 4, 5); Java 5.0: "vararg-parameter" static int sum(int val) { int res = 0; for (int i = 0; i < val.length; i++) res += val[i]; return res; int x = sum(1, 2, 3, 4, 5); vararg-parameter muss der letzte formale Parameter sein 10