Hilfe 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Ausnahmebehandlung.java
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
/** * Ausnahmebehandlung gibt die Anzahl der Tage eines Monats aus. * Demonstriert try-catch-throw. * Vergleiche das Programm Fallunterscheidung. * Beispielprogramm zur Programmiertechnik 1, Teil 3. * @author H.Drachenfels * @version 19.5.2020 */
public final class Ausnahmebehandlung {
private Ausnahmebehandlung() { }
private static final Scanner EINGABE = new Scanner(System.in);
/** * main ist der Startpunkt des Programms. * @param args wird nicht verwendet. */
public static void main(String[] args) {
System.out.print("Monat eingeben [1-12]: ");
try {
int month = EINGABE.nextInt();
if (month < 1 || month > 12) {
throw new Exception("Fehler: kein Monat");
}
switch (month) {
case 2:
System.out.println("28 oder 29 Tage");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("30 Tage");
break;
default:
System.out.println("31 Tage");
}
} catch (InputMismatchException x) {
System.err.println("Fehler: keine Zahl");
} catch (NoSuchElementException x) {
System.err.println("Fehler: keine Eingabe");
} catch (Exception x) {
// throw new Exception(...) springt hier hin
System.err.println(x.getMessage());
} finally {
EINGABE.close();
}
}
}
Title
Message
OK
Nochmal