@@ -14,7 +14,7 @@ public String findStatusAvoid() {
14
14
if (status .isPresent ()) {
15
15
return status .get ();
16
16
} else {
17
- throw new NoSuchElementException ();
17
+ throw new NoSuchElementException ("Status cannot be found" );
18
18
}
19
19
}
20
20
@@ -24,7 +24,10 @@ public String findStatusPrefer1() {
24
24
// fetch an Optional prone to be empty
25
25
Optional <String > status = Optional .empty ();
26
26
27
- return status .orElseThrow (IllegalStateException ::new );
27
+ return status .orElseThrow (
28
+ () -> new IllegalStateException ("Status cannot be found" ));
29
+ // or without message
30
+ // return status.orElseThrow(IllegalStateException::new);
28
31
}
29
32
30
33
// Prefer (before Java 10)
@@ -33,7 +36,8 @@ public String findStatusPrefer2() {
33
36
// fetch an Optional prone to be empty
34
37
Optional <String > status = Optional .empty ();
35
38
36
- return status .orElseThrow (NoSuchElementException ::new );
39
+ return status .orElseThrow (
40
+ () -> new IllegalStateException ("Status cannot be found" ));
37
41
}
38
42
39
43
// Prefer (Java 10+)
@@ -42,6 +46,6 @@ public String findStatusPrefer3() {
42
46
// fetch an Optional prone to be empty
43
47
Optional <String > status = Optional .empty ();
44
48
45
- return status .orElseThrow (); // NoSuchElementException
49
+ return status .orElseThrow (); // NoSuchElementException
46
50
}
47
51
}
0 commit comments