 |
Die finally-Klausel Die try...catch-Anweisung besitzt als optionale Möglichkeit die finally-Klausel, die als letzter Bestandteil einer try...catch-Anweisung verwendet werden kann. Sie wird immer aufgerufen, wenn die try-Klausel ausgeführt wurde, ganz gleich welches Ereignis dafür verantwortlich war. Beispiel: public class Exc006 { public static void main(String[] args) { int a=60, c, i; try { for(i=4;i>=-4;i--) { c=a/i; System.out.println("Quotient: " + c); } } catch(ArithmeticException e) { System.out.println("Division durch Null."); } finally { System.out.println("Eine Exception wurde aufgerufen."); }
}
}
Als Ausgabe erhält man:
Quotient: 15
Quotient: 20
Quotient: 30
Quotient: 60
Division durch Null.
Eine Exception wurde aufgerufen.
|
|
|