5. előadás
Mellékhatásos kifejezések
Legyen in egy bemeneti adatfolyam, pl. megnyitott fájl
Hibajelzés
Hibajelzés kivételek segítségével
public class Time {
public int hour;
public void setHour(int hour){
if(hour < 0 || hour >= 24){
throw new IllegalArgumentException("Invalid hour!");
}
this.hour = hour;
}
}
Hibajelzés assert segítségével
public class Time {
public int hour;
public void setHour(int hour){
assert hour >= 0 && hour < 24;
this.hour = hour;
}
}
Szükséges az assert-ek bekapcsolása:
Megjegyzések
A dokumentációs megjegyzések belekerülnek a generált dokumentációba (kódismeretető dokumentáció)
Dokumentációs megjegyzések
/**
* Sets the hour property. Only pass an {@code hour}
* satisfying {@code 0 <= hour <= 23}
* @param hour The value to be set
* @throws IllegalArgumentException
* If the supplied value is not between 0 and 23.
*/
Kivételek
Unchecked exception
- pl.: NullPointerException, ArrayIndexOutOfBoundsException
- Dinamikus szemantikai hiba
- "Bárhoz" keletkezhet
Checked exception
- A programszövegben jelölni KELL a terjedést
- Ennek a konzisztenciáját ellenőrzi a fordítóprogram, fordítási-időben
- pl.:
java.sql.SQLException
class Main{
public static void main( String[] args ){
try{
Time wakeUp = Time.readTime("time.txt");
} catch(IOException e){
// Kivételt mindenképp le kell kezelni
System.err.println("Could not read wake-up time.")
}
}
}
Easter egg: felkopogók (az ébresztő óra "elődei")
public class Receptionist {
public Time[] readWakeupTimes( String[] fnames ){
Time[] times = new Time[fnames.length];
for( int i = 0; i < fnames.length; ++i ){
try {
times[i] = readTime(fnames[i]);
} catch( java.io.IOException e ){
times[i] = null; // no-op
System.err.println("Could not read " + fnames[i]);
}
}
return times; // maybe sort times before returning?
}
}
class Time{
public static Time parse(String str) {
String errorMessage;
try {
String[] parts = str.split(":");
int hour = Integer.parseInt(parts[0]);
int minute = Integer.parseInt(parts[1]);
return new Time(hour, minute);
}
catch (NullPointerException e) {
errorMessage = "Null parameter is not allowed!";
}
catch (ArrayIndexOutOfBoundsException e) {
errorMessage = "String must contain\":\"!";
}
catch (NumberFormatException e) {
errorMessage = "String must contain two numbers!";
}
throw new IllegalArgumentException(errorMessage);
}
}
class Time{
public static Time parse(String str) {
String errorMessage;
try {
String[] parts = str.split(":");
int hour = Integer.parseInt(parts[0]);
int minute = Integer.parseInt(parts[1]);
return new Time(hour, minute);
}
catch (NullPointerException
| ArrayIndexOutOfBoundsException
| NumberFormatException e) {
throw new IllegalArgumentException("Can't parse time!");
}
}
}
class Time{
public static Time readTime(String fname) throw IOException {
BufferedReader in = new BufferedReader(new FileReader(fname));
Time time;
try {
String line = in.readLine();
time = parse(line)
} finally{
in.close();
}
return time;
}
}
Try-with-resources
class Main{
public static void main(String[] args){
try (BufferedReader in = ...) {
String line = in.readLine();
return parse(line);
// Lezárás automatikusan megtörténik
}
}
}
Cursed:
class Main{
public static void main(String[] args){
try (BufferedReader in = ...) {
String line = in.readLine();
return parse(line);
// Lezárás automatikusan megtörténik
} catch(IOException | IllegalArgumentException e){
System.out.println(e);
return new Time(0, 0);
}
}
}
Több erőforrás: