/****************************************************************/ /* Prof. Dr. Carsten Vogt */ /* FH Koeln, Fak. 07 / Nachrichtentechnik */ /* http://www.nt.fh-koeln.de/vogt */ /* */ /* Das Programm demonstriert die Ausnahmebehandlung bei Arrays: */ /* einfache Ausnahmebehandlung mit nur einem Handler, */ /* dabei Gegenueberstellung zum Verhalten im Fall ohne aus- */ /* programmierten Handler */ /****************************************************************/ import java.io.*; public class Array { static int[] feld = {2,4,6,8,10}; /* Feld mit fuenf Eintraegen */ /* ausgabe_mit(): Methode zur Ausgabe des i-ten Eintrags von feld MIT Ausnahmebehandlung */ private static void ausgabe_mit(int index) { try { System.out.println("feld["+index+"] = "+feld[index]); } catch (IndexOutOfBoundsException e) { System.out.println("FEHLER - Index nicht im Bereich [0,4]"); System.out.println(" Index: "+e.getMessage()); } } /* ausgabe_ohne(): Methode zur Ausgabe des i-ten Eintrags von feld OHNE Ausnahmebehandlung */ private static void ausgabe_ohne(int index) { System.out.println("feld["+index+"] = "+feld[index]); } public static void main(String[] args) { System.out.println(); System.out.println("Array-Ausgabe mit Ausnahmebehandlung:"); System.out.println("-------------------------------------"); System.out.println(); for(int i=-2;i<=6;i++) ausgabe_mit(i); System.out.println(); System.out.println(); System.out.println("Array-Ausgabe ohne Ausnahmebehandlung:"); System.out.println("--------------------------------------"); System.out.println(); for(int i=-2;i<=6;i++) ausgabe_ohne(i); } }