1. Exceptions - Ausnahmebehandlung
class myClass {
public int quotient(int a, b) {
try {
return a / b; // Hier tritt eine Exception auf,
} // wenn b 0 ist
catch (ArithmeticException e) {
System.err.println("Err: " + e); // Fehlermeldung ausgeben
return -1; // Auf die Exception reagieren
}
}
}
|
Verzicht auf lokale Fehlerbehandlung:
class myClass {
public int quotient(int a, b) throws Arithmetic Exception {
return a / b; // Eine Exception wird an den nächst-
} // höheren Programmblock weitergeleitet
}
|