-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #342 from Wave1994-Hoon/week12-tmp
[#71][2κΈ°] νμμλ κ²μ¬ μμΈ μ¬μ©μ νΌνλΌ
- Loading branch information
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
10μ₯/71_νμ_μλ_κ²μ¬_μμΈ_μ¬μ©μ_νΌνλΌ_κΉκ΄ν.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Item 71. νμμλ κ²μ¬ μμΈ μ¬μ©μ νΌνλΌ | ||
## 1. ν΅μ¬μ 리 | ||
- κΌ νμν κ³³μλ§ μ¬μ©νλ€λ©΄, κ²μ¬ μμΈλ νλ‘κ·Έλ¨μ μμ μ±μ λμ¬μ€λ€. νμ§λ§ λ¨μ©νλ©΄ μ°κΈ° μ΄λ €μ΄ API λ₯Ό λ³λλ€. | ||
- μμΈ μν©μμ 볡ꡬν λ°©λ²μ΄ μλ€λ©΄ `λΉκ²μ¬ μμΈ`λ₯Ό λμ§μ. | ||
- λ³΅κ΅¬κ° κ°λ₯νκ³ νΈμΆμκ° κ·Έ μ²λ¦¬λ₯Ό ν΄μ£ΌκΈΈ λ°λλ€λ©΄, μ°μ μ΅μ λμ λ°νν΄λ λ μ§ κ³ λ―Όνμ. | ||
- μ΅μ λλ§μΌλ‘λ μν©μ μ²λ¦¬νκΈ°μ μΆ©λΆν μ 보λ₯Ό μ 곡ν μ μμ λλ§ κ²μ¬ μμΈλ₯Ό λμ§μ. | ||
|
||
<br> | ||
|
||
## 2. κ²μ¬ μμΈ (Checked Exception) | ||
- κ²μ¬ μμΈλ λ°μν λ¬Έμ λ₯Ό νλ‘κ·Έλλ¨Έκ° μ²λ¦¬νμ¬ μμ μ±μ λν μ μλ€. | ||
- λ¬Όλ‘ , κ³Όνκ² μ¬μ©νλ©΄ μ€νλ € μ°κΈ° λΆνΈν APi κ° λ μ μλ€. | ||
- (1) try/catch λΈλ‘, throws | ||
```java | ||
public class Example { | ||
|
||
public void example() { | ||
|
||
try { | ||
|
||
} catch (IOException e) { | ||
|
||
} | ||
|
||
try { | ||
|
||
} catch (IOException e) { | ||
|
||
} | ||
|
||
try { | ||
|
||
} catch (IOException e) { | ||
|
||
} | ||
|
||
... | ||
} | ||
} | ||
``` | ||
- (2) Java 8, Stream | ||
```java | ||
public class Test { | ||
|
||
public static void main(String[] args) { | ||
String[] names = {"μλ°", "νμ΄μ¬", "κ³ ", "Trigger"}; | ||
|
||
Arrays.stream(names) | ||
.map(Example::verifyName) // <--- compile error | ||
.collect(toSet()); | ||
} | ||
|
||
public static class Example { | ||
|
||
public String verifyName(String name) throws Exception { | ||
if (name == "trigger") { | ||
throw new Exception("fail"); | ||
} | ||
return name; | ||
} | ||
} | ||
|
||
} | ||
``` | ||
|
||
|
||
<br> | ||
|
||
|
||
## 3. κ²μ¬ μμΈλ₯Ό ννΌνλ λ°©λ² | ||
### (1) Optional | ||
- κ²μ¬ μμΈλ₯Ό λμ§λ λμ λ¨μν λΉ μ΅μ λμ λ°ννλ©΄ λλ€. | ||
- μμ | ||
- AS-IS | ||
```java | ||
Long userId = 1L; | ||
try { | ||
User user = userService.findUserById(userId); | ||
} catch (Exception e) { | ||
log.error("fail to find user, userId: {}", userId) | ||
user = new User(userId); | ||
} | ||
``` | ||
- TO-BE | ||
```java | ||
Long userId = 1L; | ||
|
||
User user = repository.findUserById(userId) | ||
.orElseGet(() -> new User(userId)); | ||
``` | ||
|
||
<br> | ||
|
||
### (2) λ©μλλ₯Ό λ κ°λ‘ λΆν νμ¬ λΉκ²μ¬ μμΈλ‘ λ°κΎΌλ€. | ||
- μ΄ λ°©μμμ 첫 λ²μ§Έ λ©μλλ μμΈκ° λμ Έμ§μ§ μ¬λΆλ₯Ό boolean κ°μΌλ‘ λ°ννλ€. | ||
- μμ | ||
- AS-IS | ||
```java | ||
try { | ||
obj.action(args); | ||
} catch (TheCheckedException e) { | ||
... // μμΈ μν©μ λμ²νλ€. | ||
} | ||
``` | ||
- TO-BE | ||
```java | ||
if(obj.actionPermitted(args)){ | ||
obj.action(args); | ||
} else { | ||
... // μμΈ μν©μ λμ²νλ€. | ||
} | ||
``` | ||
|
||
- νμ§λ§ μΈλΆ μμμ μνμ¬ μνκ° λ³ν μ μλ€λ©΄, μ΄ λ°©λ²μ μ μ μΉ μλ€. |