1 // TryCatchFinally.java
2 import java.util.Scanner;
3 import java.util.NoSuchElementException;
4
5 /** 6 * TryCatchFinally testet die try-catch-finally-Anweisung. 7 * Beispielprogramm zur Programmiertechnik 1, Teil 3. 8 * @author H.Drachenfels 9 * @version 16.11.2020 10 */
11 public final class TryCatchFinally {
12 private TryCatchFinally() { }
13
14 private static final Scanner EINGABE = new Scanner(System.in);
15
16 /** 17 * main ist der Startpunkt des Programms. 18 * @param args wird nicht verwendet. 19 */
20 public static void main(String[] args) {
21 try {
22 System.out.print("Eingabe: ");
23 int n = EINGABE.nextInt();
24 if (n < 0) {
25 System.out.println(1);
26 return;
27 }
28 System.out.println(2);
29 } catch (NoSuchElementException x) {
30 if (!EINGABE.hasNext()) {
31 System.out.println(3);
32 return;
33 }
34 System.out.println(4);
35 } finally {
36 System.out.println(5);
37 }
38
39 System.out.println(6);
40 }
41 }
42