und Laufzeitfehler – Beispiele (1) - COGA

Transcrição

und Laufzeitfehler – Beispiele (1) - COGA
Themen der Übung
Exceptions
1
Organisatorisches
2
Compilezeit- und Laufzeitfehler
3
Exceptions
4
Try-Catch-Finally
CoMa-Übung VI
TU Berlin
28.11.2012
Bewertung der OA 5 fehlerhaft, Madeleine arbeitet dran
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
1 / 19
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
2 / 19
Compilezeit- und Laufzeitfehler – Beispiele (1)
Compilezeit- und Laufzeitfehler
Compilezeitfehler
Compilezeitfehler sind Fehler, die der Java-Kompiler (javac) beim
Kompilieren eines Java-Programms findet.
1
public class Fraction {
2
p r i v a t e l o n g num ;
p r i v a t e l o n g denom ;
3
int i = 1.0:
4
5
int() array == null;
public Fraction multiply ( r ) {
t h i s . num = t h i s . num ∗ r . num
t h i s . denom = t h i s . denom ∗ r . denom
}
6
7
Laufzeitfehler
8
Laufzeitfehler sind Fehler, die Java (java) beim Ausführen eines
Java-Programms findet.
9
10
}
int i = 1/0;
Fehler
Andere Fehler
Fehlende Kommas in Zeilen 7 & 8 → Compilefehler
Es gibt Fehler, die weder der Java-Kompiler noch Java beim Ausführen
findet. Solche Fehler sind weder Compilezeitzeit noch Laufzeitfehler.
Fehlendes return / falscher Rückgabetyp → Compilefehler
Endlosschleifen
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
3 / 19
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
4 / 19
Compilezeit- und Laufzeitfehler – Beispiele (2)
1
Compilezeit- und Laufzeitfehler – Beispiele (3)
1
public class Fraction {
2
4
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
d o u bl e d = 0 . 0 ;
System . o u t . p r i n t l n ( ( S t r i n g ) 1 . 0 ) ;
System . o u t . p r i n t l n ( ( S t r i n g ) d ) ;
System . o u t . p r i n t l n ( ( i n t ) Double . NaN) ;
System . o u t . p r i n t l n ( ( l o n g ) ( doubl e ) ( l o n g ) Double .
POSITIVE INFINITY ) ;
}
3
p r i v a t e l o n g num ;
p r i v a t e l o n g denom ;
3
4
5
5
6
p u b l i c Void sum ( F r a c t i o n r ) {
num += r . num ;
denom += r . denom ;
}
6
7
8
9
10
public c la s s HelloWorld {
2
7
8
9
}
10
Fehler
}
Fehler
Void ist kein Rückgabetyp → Compilefehler
Zeilen 5 & 6 sind nicht konvertibel → Compilefehler
Falsche Implementierung → Weder noch
CoMa-Übung VI (TU Berlin)
Exceptions
Zeilen 7 & 8 funktionieren → Ergeben 0 und 263 − 1
28.11.2012
Compilezeit- und Laufzeitfehler – Beispiele (4)
1
public c la s s HelloWorld {
public
int
int
for
4
5
6
7
8
1
10
12
s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
[ ] l e n g t h s = new i n t [ a r g s . l e n g t h ] ;
total ;
( i n t i =0; i <l e n g t h s . l e n g t h ; i ++) {
lengths [ i ] = args . length ;
t o t a l += l e n g t h s [ i ] ;
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
i n t [ ] l e n g t h s = new i n t [ a r g s . l e n g t h ] ;
do ubl e t o t a l = 0 . 0 ;
f o r ( i n t i =0; i <=l e n g t h s . l e n g t h ; i ++) {
t o t a l += a r g s . l e n g t h ( ) ;
}
System . o u t . p r i n t l n ( ” D u r c h s c h n i t t : ” + t o t a l / a r g s
. length ) ;
}
4
5
6
7
8
9
10
11
}
}
Fehler
Fehler
Array hat keine length() Methode → Compilefehler
Variable total nicht initialisiert → Compilefehler
ArrayIndexOutOfBoundsException in der Schleife → Laufzeitfehler
Potentielle ArithmeticException in Zeile 10 → Laufzeitfehler
CoMa-Übung VI (TU Berlin)
6 / 19
public c la s s HelloWorld {
3
}
11
28.11.2012
2
}
System . o u t . p r i n t l n ( ” D u r c h s c h n i t t : ” + t o t a l / a r g s
. length ) ;
9
Exceptions
Compilezeit- und Laufzeitfehler – Beispiele (5)
2
3
CoMa-Übung VI (TU Berlin)
5 / 19
Exceptions
28.11.2012
7 / 19
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
8 / 19
Compilezeit- und Laufzeitfehler – Beispiele (6)
1
Compilezeit- und Laufzeitfehler – Beispiele (7)
public c la s s HelloWorld {
1
2
i n t [ ] numbers2 ;
3
4
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
i n t [ ] numbers = new i n t [ a r g s . l e n g t h ] ;
f o r ( i n t i =0; i <numbers . l e n g t h ; i ++) {
numbers = I n t e g e r . p a r s e I n t ( a r g s ) ;
numbers2 = numbers ∗ numbers ;
}
}
5
6
7
8
9
10
11
6
7
8
9
10
11
}
Fehler
NumberFormatException in Zeile 8 möglich → Laufzeitfehler
numbers2 nicht statisch → Compilefehler
NullPointerException in Zeile 9 möglich → Laufzeitfehler
* funktioniert nicht mit Arrays → Compilefehler
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
9 / 19
Exceptions
CoMa-Übung VI (TU Berlin)
1
Exceptions sind Javas Konzept, um mit Fehlern und außergewöhnlichen
Situationen umzugehen.
2
3
4
5
4
5
6
Exceptions
28.11.2012
10 / 19
Exceptions (2)
Exceptions
3
}
Fehler
Integer.parseInt funktioniert nicht mit Arrays → Compilefehler
2
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
i n t [ ] numbers = new i n t [ a r g s . l e n g t h ] ;
f o r ( i n t i =0; i <numbers . l e n g t h ; i ++) {
numbers [ i ] = I n t e g e r . p a r s e I n t ( a r g s [ i ] ) ;
numbers2 [ i ] = numbers [ i ] ∗ numbers [ i ] ;
}
}
5
12
1
s t a t i c i n t [ ] numbers2 ;
3
4
12
public c la s s HelloWorld {
2
try {
// Code , d e r e i n e E x c e p t i o n a u s l o e s e n kann
} catch ( . . . ) {
// Code zum B e h a n d e l n d e r E x c e p t i o n
}
// Es g e h t n o r m a l w e i t e r , d i e E x c e p t i o n wurde b e h a n d e l t
6
S t r i n g s t r i n g T o C o n v e r t = ”42%” ;
try {
Integer . parseInt ( stringToConvert ) ;
} c a t c h ( Num b erFo rma t Ex ce pt io n e ) {
System . o u t . p r i n t l n ( s t r i n g T o C o n v e r t + ” kann n i c h t i n
e i n e Z a h l k o n v e r t i e r t werden ! ” ) ;
}
Exceptions und Javadocs
Werden in einer Methode Exceptions ausgelöst, sollten sie im Javadoc
dokumentiert sein. Das ist für die offiziellen Java-Klassen auch der Fall,
wie etwa für Integer.parseInt.
try-catch-finally
Exceptions werden in try-catch-finally behandelt
Erlaubt das Behandeln von Ausnahmen (catch)
Exceptions und Laufzeitfehler
Erlaubt garantierte Aufräumarbeiten (finally)
Hinweis: Tritt eine Exception auf, liegt ein Laufzeitfehler vor.
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
11 / 19
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
12 / 19
Exceptions (3)
1
2
3
4
5
6
Exceptions (4)
S t r i n g s t r i n g T o C o n v e r t = ”42%” ;
try {
Integer . parseInt ( stringToConvert ) ;
} c a t c h ( N u m b e r F o r m a tExc e ptio n e ) {
System . o u t . p r i n t l n ( s t r i n g T o C o n v e r t + ” kann n i c h t i n
e i n e Z a h l k o n v e r t i e r t werden ! ” ) ;
}
1
2
3
4
5
6
S t r i n g s t r i n g T o C o n v e r t = ”42%” ;
try {
Integer . parseInt ( stringToConvert ) ;
} c a t c h ( Num b erFo rma t Ex c ept io n e ) {
System . o u t . p r i n t l n ( s t r i n g T o C o n v e r t + ” kann n i c h t i n
e i n e Z a h l k o n v e r t i e r t werden ! ” ) ;
}
Exceptions – Ablauf
Exceptions – Eigenschaften
Tritt eine Exception auf,
Ein Exception-Objekt e hat,
wird ein Exception-Objekt erzeugt,
eine Klasse mit einem Namen e.getClass().getName(),
die Ausführung des Codes unterbrochen,
eine Nachricht e.getMessage(),
die erste passende Catch-Klausel gesucht,
eine Möglichkeit, den Stack-Trace auf der Konsole auszugeben
(e.printStackTrace();).
und dort mit der Code-Ausführung fortgefahren.
Nach Beenden der Catch-Klausel werden alle anderen Catch-Klauseln
übersprungen. Tritt keine Exception auf, werden Catch-Klauseln nicht ausgeführt.
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
14 / 19
Exceptions – throws
} catch ( . . . ) {
Kann eine Methode eine Exception werfen, so wird das üblicherweise per
throws in der Methodendeklaration angegeben.
} catch ( . . . )
{
6
7
28.11.2012
try {
4
5
Exceptions
Exceptions (6)
2
3
CoMa-Übung VI (TU Berlin)
13 / 19
Exceptions (5)
1
Wird eine Exception nicht gefangen, gibt Java alles drei auf der Konsole
aus.
1
}
2
3
i n t d i v i d e ( i n t a , i n t b ) throws A r i t h m e t i c E x c e p t i o n {
return a / b ;
}
Exceptions – Code-Wiederholungen
try-catch-finally
Ist eine Exception aufgetreten und in einem Catch-Block behandelt
worden, gibt es keine Möglichkeit, automatisch an die Stelle
zurückzuspringen, wo die Exception aufgetreten ist.
Müssen bestimmte Aufräumarbeiten in jedem Fall durchgeführt werden,
kann ein Try-Catch-Block mittels eines Finally-Blocks ergänzt werden.
finally ist optional.
Exceptions – Mehrere Catch-Blöcke
Maximal 1x finally pro try.
Zu einem try können mehrere Catch-Blöcke gehören, wenn
unterschiedliche Exceptions behandelt werden müssen.
finally-Code wird immer ausgeführt, egal, ob der try-Block durch
return, eine Exception oder normal beendet wurde.
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
15 / 19
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
16 / 19
Exceptions (7)
1
Exceptions (7)
try {
1
2
3
2
} catch ( . . . ) {
3
4
5
} finally
4
{
5
6
7
try {
return 1;
} finally {
return 0;
}
}
Warnung
Hinweis
1
try-finally kann auch ohne catch benutzt werden.
try-finally behandelt keine Exceptions, die würden an den
Aufrufer weitergereicht.
Wird oft im Umgang mit Dateien verwendet.
Behandelt niemand die Exception, wird das Programm beendet.
In catch & finally Blöcken können neue Exceptions geworfen
werden – dann sind eventuell verschachtelte try-Blöcke erforderlich.
try {
2
3
} finally
Ein return im finally-Block überschreibt ein return im
try-Block.
{
4
5
}
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
17 / 19
Exceptions (8)
Eigene Exceptions
Eigene Exceptions werden mittels throw ausgelöst.
Es ist sinnvoll, direkt am Anfang einer Methode die Eingaben zu
testen.
→ je schneller Fehler gefunden werden, desto besser (fail-fast).
1
2
3
4
5
6
d ou bl e s q r t ( d o u b l e d ) {
i f (d < 0 ) {
throw new I l l e g a l A r g u m e n t E x c e p t i o n ( ” K e i n e n e g a t i v e n
Zahlen e r l a u b t ! ” ) ;
}
r e t u r n Math . s q r t ( d ) ;
}
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
19 / 19
CoMa-Übung VI (TU Berlin)
Exceptions
28.11.2012
18 / 19

Documentos relacionados