-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exceptions
48 lines (47 loc) · 1.59 KB
/
Exceptions
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
import java.util.Scanner;
import java.util.*;
class SmallException extends Exception {
public SmallException (String message) {
super(message);
}
}
class BigException extends Exception {
public BigException (String message) {
super(message);
}
}
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int testedNumber = 0;
boolean inputCorrect = true;
do {
try {
System.out.print("Type in the number for testing: ");
testedNumber = reader.nextInt();
inputCorrect = true;
} catch (InputMismatchException e) {
System.out.println("You did not type in an integer!");
inputCorrect = false;
reader.nextLine();
}
} while(!inputCorrect);
try {
testValue(testedNumber);
System.out.println("Value was between 5 and 10.");
} catch(SmallException e) {
System.out.println("SmallException caught!");
printErrorReport(e);
} catch(BigException e) {
System.out.println("BigException caught!");
printErrorReport(e);
}
}
public static void printErrorReport(Exception e){
System.out.println(e.getMessage());
}
public static void testValue(int value) throws SmallException, BigException{
if (value<5) throw new SmallException("Value is lower than 5");
else if (value>10) throw new BigException("Value is higher than 10");
}
}