Exception Handling in Java

Preview:

DESCRIPTION

Exception Handling in Java. Seminar aus Softwareentwicklung Norbert Wiener. Ziele von Exception Handling. Programme robuster machen gegen: • fehlerhafte Eingaben • Gerätefehler (Defekte, Platte voll, ...) • Programmierfehler (z.B. Division durch 0) - PowerPoint PPT Presentation

Citation preview

Exception Handling in Java

Seminar aus Softwareentwicklung

Norbert Wiener

Ziele von Exception Handling

Programme robuster machen gegen: • fehlerhafte Eingaben

• Gerätefehler (Defekte, Platte voll, ...) • Programmierfehler (z.B. Division durch 0)

Trennung zwischen Algorithmus und Fehlerbehandlung

Fehlerbehandlung durch Compiler überprüfbar machen

Auslösen von Exception explizite Ausnahmen: „throw“ Anweisung im Code.

implizite Ausnahmen durch die JVM- Divison durch 0 => ArithmeticException- Zugriff über null-Ref. => NullPointerException- Ungültige Konvertierung => ClassCastException

setPassword(String pw) { ......

if(pw.length() < 5) throw new Exception(„mind. 5 Zeichen“); .......}

int intDiv (int i, int j) { return i/j; }

try-catch-finally Syntaxtry { Anweisungen;} catch (Ausnahme1 a1) { Fehlerbehandlung1;} catch (Ausnahme2 a2) { Fehlerbehandlung2;} finally { Aufräumen;}

Der finally-Block ist optional

Nach einem try-Block muss mindestens ein catch-Block folgen!

Bei mehreren catch-Blöcken beginnt die Suche der Ausnahmenbehandlung von

oben nach unten.

Beispiel

class Catch{ public static void main(String[] args){ try{ int i = Integer.parseInt(args[0]); System.out.print("i= "+i); }catch(ArrayIndexOutOfBoundsException e){ System.out.print("Parameter vergessen!"); }catch(NumberFormatException e){ System.out.print(" kein int-Wert!"); }finally{ System.out.println(" finally!"); } }}

Catch 123i= 123 finally!

Catch xykein int-Wert! finally!

CatchParameter vergessen! finally!

Finally wird immer ausgeführt!

Ausnahme-Klasse definieren

class MyException extends Exception{ public MyException() { super(); } public MyException(String msg) { super(msg); }}

Konstuktor ohne Parameter

Konstruktor mit Fehlermeldung

Eigener Exceptiontyp (indirekt) von

Throwable ableiten.

class MyException extends Exception{}

Die throws-Klausel

Weitergabe der Exception an die aufrufende Methode

setPassword(String pw) { try{ if(pw.length() < 5){ throw new Exception(„Fehler“); } }catch(Exception e){ Ausnahme behandeln! }}

static setPassword(String pw) throws Exception { if(pw.length() < 5){ throw new Exception(„Fehler“); }.......}

Try{ setPassword(pw);}catch(Exception e){ Ausnahme behandeln!}

throws Beispiel

..Main(...){

...... try{ A(x); }catch(Exc1 e)

{ ..print("Main");}........

........

A(int x)

throws Exc1{

try{

B(x);

}catch(Exc2 e){

...println("A");

}finally{

..println("finA")

}

}

........

B(int x)

throws Exc1, Exc2{

try{

C(x);

}catch(Exc3 e){

...println("B");

}finally{

...println("finB");

}

}

C(int x)

throws Exc1, Exc2 ,Exc3 {

try{

if(x==1){

throw new Exc1("F.");

}else if(x==2){

throw new Exc2();

}else if(x==3){

throw new Exc3();

}else{

throw new Exc4();

}

}catch(Exc4 e){

....println("C");

}

}

x=1

finB

finA

Main

x=2

finB

A

finA

x=3

B

finB

finA

x=4

C

finB

finA

Debugging von Ausnahmen

finBfinAMainFehler!Exc1: Fehler! at C.<init>(C.java:6) at B.<init>(B.java:4) at A.<init>(A.java:4) at Test.main(Test.java:6)

class Test{

static int x =1;

public static void main(String args[]) {

try{

A a= new A(x);

}catch(Exc1 e){

System.out.println("Main");

System.out.println(e.getMessage());

e.printStackTrace();

}

}

}

e.getMessage()

e.printStackTrace()

KlassenhierarchieThrowable

Error Exception

VirtualMachineError LinkageError

OutOfMemoryError

RuntimeException

NullpointerException

IOException

FileNotFoundExceptionNoSuchFieldError

unchecked

checked

Die Error-Klasse VirtualMachineError InternalError OutOfMemoryError StackOverflowError UnknownError LinkageError NoSuchMethodError NoSuchFieldError AbstraktMethodError ThreadDeath

Ausnahmen des Typs Error sind nicht vernünftig zu behandeln.

Die Exception Klasse IOException FileNotFoundException SocketException EOFException RemouteException UnknownHostException InterruptedException NoSuchFieldException ClassNotFoundException NoSuchMethodException

Diese Exception müssen behandelt

werden!(ausgenommen RuntimeException)

Exception auf ByteCode Ebene

void f() { try { throw new Exception(); } catch (Exception e) { System.out.println(e); }}

Exception e;e = new Exception();throw e;

0 new #2 <Class Exception> 3 dup 4 invokespecial <init> 7 athrow 8 astore_1 9 getstatic out:System12 aload_113 invokevirtual println16 return

Objekt im Speicher allokierenObjektreferenz auf OperantenStack

Referenzadresse wird dupliziert

Konstruktor wird ausgeführt

Exception Table:

From To Target Type

0 8 8 Exception

Adr.-> Exception

Adr.-> Exception

Operanden-Stack

Beispiel

static int remainder(int dividend, int divisor) throws OverflowException, DivideByZeroException

{ if ((dividend == Integer.MIN_VALUE) && (divisor == -1)) { throw new OverflowException(); } try { return dividend % divisor; } catch (ArithmeticException e) { throw new DivideByZeroException(); }}

Exception Handling wird an die

Vorgängermethode delegiert!

Beispiel 0 iload_0 1 ldc #1 <Integer - 2147483648> 3 if_icmpne 19 6 iload_1 7 iconst_m1 8 if_icmpne 19

11 new #4 <Class OverflowException>

14 dup15 invokespecial #10 <Method

OverflowException()>18 athrow

19 iload_020 iload_121 irem22 ireturn

23 pop24 new #2 <Class DivideByZeroE.>27 dup28 invokespecial #9 <Method

DivideByZeroException()>31 athrow

Exception Table:

From To Target Type

19 23 23 ArithmeticException

static int remainder(int dividend, int divisor)throws OverflowException, DivideByZeroException { if ((dividend == Integer.MIN_VALUE) && (divisor == -1)) { throw new OverflowException(); } try { return dividend % divisor; } catch (ArithmeticException e) { throw new DivideByZeroException(); }}

Danke für die Aufmerksamkeit

Recommended