Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Gradle
- oracle ansi
- oracle ansi sql
- ansi sql 장점
- BFS
- SQL
- 프로그래머스
- 프로그래머스 이중우선순위큐 java
- ansi sql 단점
- DBASE&
- Spring Boot
- 이중우선순위큐 java
- 디베이스앤
- 디베이스앤 인턴 후기
- IT 면접 준비
- DFS
- JPA
- 프로그래머스 이중우선순위큐 자바
- CJ DBASE&
- 그리디
- DP
- 이분탐색
- 이중우선순위큐 자바
- 면접 필수 질문
- Java
- 개발자 면접 준비
- 백준
- 위상정렬
- 프로그래머스 이중우선순위큐
- 백트래킹
Archives
- Today
- Total
쉬운 프로그래밍
Optional을 통한 예외처리 본문
public Long join(Member member) {
//중복 예외처리
Optional<Member> result = memberRepository.findById(member.getId());
result.ifPresent(m -> {
throw new IllegalStateException("이미 존재하는 회원입니다.");
});
}
위와 같이 Optional을 바로 return받아서 예외처리를 할 수 있지만 이는 좋은 방법이 아니다.
public Long join(Member member) {
// Optional 더 깔끔하게
this.memberRepository.findById(member.getId())
.ifPresent(m -> {
throw new IllegalStateException("이미 존재하는 회원입니다.");
});;
this.memberRepository.save(member);
return member.getId();
}
이거처럼 Optional을 따로 반환받지 않고 바로 Exception을 터뜨리는게 더 깔끔하다.
'Programming > Java' 카테고리의 다른 글
[Unirest] Unirest를 통한 REST API 호출 (1) | 2021.05.30 |
---|---|
Logback과 Slf4j에 대해서 알아보자. (1) | 2021.04.20 |
[JAVA] uses unchecked or unsafe operations. 에러 (0) | 2021.04.06 |
Comments