Exception Handling in Java - PowerPoint PPT Presentation

About This Presentation
Title:

Exception Handling in Java

Description:

Exception Handling in Java Seminar aus Softwareentwicklung Norbert Wiener – PowerPoint PPT presentation

Number of Views:676
Avg rating:3.0/5.0
Slides: 17
Provided by: Bert2150
Category:

less

Transcript and Presenter's Notes

Title: Exception Handling in Java


1
Exception Handling in Java
  • Seminar aus Softwareentwicklung
  • Norbert Wiener

2
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

3
Auslösen von Exception
  • explizite Ausnahmen throw Anweisung im
    Code.
  • implizite Ausnahmen durch die JVM- Divison durch
    0 gt ArithmeticException- Zugriff
    über null-Ref. gt NullPointerException-
    Ungültige Konvertierung gt ClassCastException

setPassword(String pw) ......
if(pw.length() lt 5) throw new Exception(mind. 5
Zeichen) .......
int intDiv (int i, int j) return i/j
4
try-catch-finally Syntax
  • try
  • Anweisungen
  • catch (Ausnahme1 a1)
  • Fehlerbehandlung1
  • catch (Ausnahme2 a2)
  • Fehlerbehandlung2
  • finally
  • Aufräumen

Nach einem try-Block muss mindestens ein
catch-Block folgen!
Der finally-Block ist optional
Bei mehreren catch-Blöcken beginnt die Suche der
Ausnahmenbehandlung von oben nach unten.
5
Beispiel
  • class Catch
  • public static void main(String args)
  • try
  • int i Integer.parseInt(args0)
  • 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 123 i 123 finally! Catch xy kein
int-Wert! finally! Catch Parameter vergessen!
finally!
Finally wird immer ausgeführt!
6
Ausnahme-Klasse definieren
Eigener Exceptiontyp (indirekt) von Throwable
ableiten.
  • class MyException extends Exception
  • class MyException extends Exception
  • public MyException()
  • super()
  • public MyException(String msg)
  • super(msg)

Konstuktor ohne Parameter
Konstruktor mit Fehlermeldung
7
Die throws-Klausel
setPassword(String pw) try
if(pw.length() lt 5) throw new
Exception(Fehler)
catch(Exception e) Ausnahme behandeln!
  • Weitergabe der Exception an die aufrufende Methode

static setPassword(String pw)
throws Exception if(pw.length() lt 5)
throw new Exception(Fehler) .......
Try setPassword(pw)catch(Exception e)
Ausnahme behandeln!
8
throws Beispiel
C(int x) throws Exc1, Exc2 ,Exc3 try
if(x1) throw new
Exc1("F.") else if(x2)
throw new Exc2() else if(x3)
throw new Exc3() else
throw new Exc4() catch(Exc4 e)
....println("C")
........ 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")
  • ..Main(...)
  • ......
  • try
  • A(x)
  • catch(Exc1 e)
  • ..print("Main")
  • ........

x1 finB finA Main
x2 finB A finA
x3 B finB finA
x4 C finB finA
9
Debugging von Ausnahmen
e.getMessage() e.printStackTrace()
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()
  • finB
  • finA
  • Main
  • Fehler!
  • Exc1 Fehler!
  • at C.ltinitgt(C.java6)
  • at B.ltinitgt(B.java4)
  • at A.ltinitgt(A.java4)
  • at Test.main(Test.java6)

10
Klassenhierarchie
Throwable
Error
Exception
VirtualMachineError
LinkageError
RuntimeException
IOException
NullpointerException
FileNotFoundException
OutOfMemoryError
NoSuchFieldError
unchecked
checked
11
Die Error-Klasse
  • VirtualMachineError
  • InternalError
  • OutOfMemoryError
  • StackOverflowError
  • UnknownError
  • LinkageError
  • NoSuchMethodError
  • NoSuchFieldError
  • AbstraktMethodError
  • ThreadDeath

Ausnahmen des Typs Error sind nicht vernünftig zu
behandeln.
12
Die Exception Klasse
  • IOException
  • FileNotFoundException
  • SocketException
  • EOFException
  • RemouteException
  • UnknownHostException
  • InterruptedException
  • NoSuchFieldException
  • ClassNotFoundException
  • NoSuchMethodException
  • Diese Exception müssen behandelt werden!
  • (ausgenommen RuntimeException)

13
Exception auf ByteCode Ebene
  • void f()
  • try
  • throw new Exception()
  • catch (Exception e)
  • System.out.println(e)

Exception e e new Exception() throw e
Adr.-gt Exception
Adr.-gt Exception
Operanden-Stack
Objekt im Speicher allokieren Objektreferenz auf
OperantenStack
0 new 2 ltClass Exceptiongt 3 dup 4
invokespecial ltinitgt 7 athrow 8 astore_1 9
getstatic outSystem12 aload_113
invokevirtual println16 return
Referenzadresse wird dupliziert
Konstruktor wird ausgeführt
Exception Table From To Target Type 0
8 8 Exception
14
Beispiel
  • static int remainder(int dividend, int divisor)
  • throws OverflowException, DivideByZeroExceptio
    n
  • 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!
15
Beispiel
  • 0 iload_0
  • 1 ldc 1 ltInteger - 2147483648gt
  • 3 if_icmpne 19
  • 6 iload_1
  • 7 iconst_m1
  • 8 if_icmpne 19
  • 11 new 4 ltClass OverflowExceptiongt
  • 14 dup
  • 15 invokespecial 10 ltMethod OverflowException()gt
  • 18 athrow
  • 19 iload_0
  • 20 iload_1
  • 21 irem
  • 22 ireturn
  • 23 pop
  • 24 new 2 ltClass DivideByZeroE.gt

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 Table From To Target
Type 19 23 23
ArithmeticException
16
Danke für die Aufmerksamkeit
Write a Comment
User Comments (0)
About PowerShow.com