-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.java
More file actions
25 lines (20 loc) · 762 Bytes
/
App.java
File metadata and controls
25 lines (20 loc) · 762 Bytes
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
package kr.springboot.examples.ex6;
import java.util.Optional;
/**
* When No Value Is Present, Throw an Explicit Exception Via orElseThrow(Supplier<? extends X> exceptionSupplier)
* 값이 없으면 orElseThrow(Supplier<? extends X> exceptionSupplier)를 통해 명시적 예외를 던져라.
*/
public class App {
// Avoid
public String findUserStatusAvoid(Long id) {
Optional<String> status = Optional.empty();
if (status.isPresent()) return status.get();
else throw new IllegalStateException();
}
//Prefer
public String findUserStatusPrefer(Long id) {
Optional<String> status = Optional.empty();
String result = status.orElseThrow(IllegalStateException::new);
return result;
}
}