class NichtGefunden extends Exception{ private static final long serialVersionUID = 7526471155622776147L; private String message; public NichtGefunden(String meldung){ message = meldung; } public String getMessage(){ return message;} } // Ende der Klasse NixDaException public class LinSuche3 { private static int such = 42; private static int[] bspArray1 = {2, 23, 16, 2, 17, 42, 2, 3, 57, 17}; private static int[] bspArray2 = {2, 23, 16, 2, 17, 56, 2, 3, 57, 17}; static int linSuche(int[] a, int x)throws NichtGefunden{ for (int i=0; i < a.length; i++) if (x == a[i]) return i; throw new NichtGefunden(" nicht gefunden !"); } public static void main(String ... args) { System.out.println("Lineare Suche 3"); System.out.print(such); int i = -1; try { i=linSuche(bspArray1, such); System.out.println(" gefunden auf Position: "+i); } catch(NichtGefunden e){System.out.println(e.getMessage()); } System.out.print(such); i = -1; try { i=linSuche(bspArray2, such); System.out.println(" gefunden auf Position: "+i); } catch(NichtGefunden e){System.out.println(e.getMessage()); } } }