-
Notifications
You must be signed in to change notification settings - Fork 0
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 #8 from Wannabe-Woowa-Article/llqqssttyy
- Loading branch information
Showing
1 changed file
with
123 additions
and
0 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
June/article/Because-of-a-Question-About-try-catch-I-Failed-My-Interview.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,123 @@ | ||
# [try-catch μ§λ¬Έ λλ¬Έμ λ©΄μ μμ νλ½νλ€](https://medium.com/@haiou-a/because-of-a-question-about-try-catch-i-failed-my-interview-2cea0225820c) | ||
|
||
### ποΈ λ²μ λ μ§: 2024.06.03 | ||
|
||
### π§ λ²μν ν¬λ£¨: λ μ(κΉλ€μ) | ||
|
||
<br/> | ||
|
||
--- | ||
|
||
`try...catch`λ μ½λ λΈλ‘μ μ€λ₯λ₯Ό μ‘μ λ μ£Όλ‘ μ¬μ©λκΈ° λλ¬Έμ λ§€μ° μΉμνκ² λκ»΄μ§κ³ , μ λ κ½€ μμ£Ό μ¬μ©ν©λλ€. | ||
|
||
νμ§λ§, μ€μ `try...catch`μ λν μ§μμ΄ λΆμ‘±νκΈ° λλ¬Έμ μλμΉ μκ² μ΄λ° κΈ°λ³Έ κ°λ μ λ©΄μ μμ λ°°μ μ΅λλ€.: `try...catch`λ λκΈ° μ½λ λΈλμμλ§ μλ¬λ₯Ό μ‘μ μ μμ΅λλ€. | ||
|
||
μ§λ¬Έμ λ€μκ³Ό κ°μμ΅λλ€: λ€μ μ½λμ λ¬Έμ κ° μλμ? κ·Έλ λ€λ©΄ μ΄λ»κ² μμ ν΄μΌ νλμ? | ||
|
||
<br/> | ||
|
||
```js | ||
try { | ||
setTimeout(() => { | ||
throw new Error('err'); | ||
}, 200); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
|
||
try { | ||
Promise.resolve().then(() => { | ||
throw new Error('err'); | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
``` | ||
|
||
<br/> | ||
|
||
'`try...catch` μ체λ λκΈ° λΈλ‘μ΄κΈ° λλ¬Έμ λΉλκΈ° μλ¬λ₯Ό μ‘μ μ μλ€.'.κΈ°λ³Έμ μΌλ‘ μ λ μ΄ μ§μ μ λͺ°λκΈ° λλ¬Έμ λ¬΄μ¨ μΌμ΄ μΌμ΄λκ³ μλμ§ μμ§ λͺ»νμ΅λλ€. κ·Έλμ μ΄ μ§λ¬Έμ λ€μμ λ νΌλμ€λ¬μ μ΅λλ€. | ||
|
||
μ€λ₯λ₯Ό μ‘κΈ° μν΄ `try...catch`λ₯Ό μ¬μ©νλ κ²μ΄ μ°λ¦¬κ° μΌλ°μ μΌλ‘ μ½λλ₯Ό μμ±νλ λ°©μμ΄ μλκ°μ? κ·Έλμ μ λ κ·Έλ₯ λͺ¨λ₯Έλ€κ³ λλ΅νκ³ μ€λ₯κ° μλ€κ³ μκ°νμ΅λλ€... λ©΄μ κ΄μ 무λ ₯ν νμ μΌλ‘ μ λ₯Ό μ³λ€λ³΄λ©° λμ€μ μ°Ύμ보λΌκ³ μ μνκ³ , κ·Έκ² λμ΄μμ΅λλ€. | ||
|
||
λΉ λ₯΄κ² μ‘°μ¬ν κ²°κ³Ό `try...catch`λ λΉλκΈ° μ½λμ μ€λ₯λ₯Ό ν¬μ°©ν μ μλ€λ μ¬μ€μ μκ² λμμ΅λλ€. | ||
|
||
**JavaScriptμμ setTimeoutμ λΉλκΈ° ν¨μμ΄λ©°, ν΄λΉ μ½λ°± ν¨μλ μ§μ λ μ§μ° ν μ΄λ²€νΈ νμ λ°°μΉλκ³ νμ¬ μ€ν μ€νμ΄ μ§μμ§ νμλ§ μ€νλ©λλ€.** | ||
|
||
λ°λΌμ setTimeout μ½λ°±μ΄ μ€νλμ΄ μ€λ₯κ° λ°μνλ©΄ try...catchλ μ΄λ―Έ μ€νμ΄ μλ£λμ΄ λΉλκΈ° μ½λ°±μ μ€λ₯λ₯Ό μ‘μ μ μμ΅λλ€. | ||
|
||
μ¬λ°λ₯Έ μ κ·Ό λ°©μμ λΉλκΈ° μμ λ΄μμ μ§μ μ€λ₯λ₯Ό μ²λ¦¬νλ κ²μ λλ€. μλ₯Ό λ€μ΄, μ½λ°± ν¨μ, Promise λλ try...catchμ κ²°ν©λ async/awaitμ μ¬μ©νλ κ² λ§μ΄μ£ . | ||
|
||
<br/> | ||
|
||
```js | ||
new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
try { | ||
throw new Error('err'); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}, 200); | ||
}) | ||
.then(() => { | ||
// μμλλ‘ μ€νλμμ λ μνλ λ‘μ§ | ||
}) | ||
.catch((err) => { | ||
console.log(err); // μλ¬λ μ¬κΈ°μ μ‘νλλ€ | ||
}); | ||
``` | ||
|
||
<br/> | ||
|
||
--- | ||
|
||
<br/> | ||
|
||
λ λ²μ§Έ μμμ κ²½μ°, `try...catch`λ₯Ό μ¬μ©ν΄ νλ‘λ―Έμ€ μ²΄μΈμμ λ°μν μ€λ₯λ₯Ό μ‘μΌλ €λ μλλ λΉν¨μ¨μ μ λλ€. μλνλ©΄ `try...catch`λ νλ‘λ―Έμ€ μ²΄μΈ λ΄μμ λΉλκΈ° μ€λ₯λ₯Ό μ‘μ μ μκΈ° λλ¬Έμ λλ€. | ||
|
||
νλ‘λ―Έμ€ κ°μ²΄λ λΉλκΈ° μμ μ μ΅μ’ μλ£(λλ μ€ν¨)μ κ·Έ κ²°κ³Ό κ°μ λνλ λλ€. νλΌλ―Έμ€μ μνλ λ€μ μ€ νλμΌ μ μμ΅λλ€: | ||
|
||
- Pending(λκΈ° μν): μ΄κΈ° μν, μ΄νλμ§λ κ±°λΆλμ§λ μμ μν. | ||
- Fulfilled(μλ£λ μν): μμ μ΄ μ±κ³΅μ μΌλ‘ μλ£λμμμ μλ―Έν©λλ€. | ||
- Reject(μ€ν¨ μν): μμ μ΄ μ€ν¨νμμ μλ―Έν©λλ€. | ||
|
||
νλ‘λ―Έμ€μμ μλ¬λ₯Ό λ°μμν€λ©΄(μ: throw λ¬Έμ ν΅ν΄) νλ‘λ―Έμ€κ° κ±°λΆ(λλ μ€ν¨)λ©λλ€. μ΄ μλ¬λ₯Ό μ¬λ°λ₯΄κ² μ²λ¦¬νλ €λ©΄ νλΌλ―Έμ€ 체μΈμμ `.catch` λ©μλλ₯Ό μ¬μ©νκ±°λ λΉλκΈ° ν¨μμμ `try...catch`λ₯Ό μ¬μ©ν΄μΌ ν©λλ€. | ||
|
||
<br/> | ||
|
||
```js | ||
// λ©μλ 1 | ||
Promise.resolve() | ||
.then(() => { | ||
throw new Error('err'); | ||
}) | ||
.catch((err) => { | ||
console.log(err); // μλ¬λ μ¬κΈ°μ μ²λ¦¬λ©λλ€. | ||
}); | ||
|
||
// λ©μλ 2 | ||
async function handleError() { | ||
try { | ||
await Promise.resolve().then(() => { | ||
throw new Error('err'); | ||
}); | ||
} catch (err) { | ||
console.log(err); // μλ¬λ μ¬κΈ°μ μ²λ¦¬λ©λλ€. | ||
} | ||
} | ||
|
||
handleError(); | ||
``` | ||
|
||
<br/> | ||
|
||
### >> κ²°λ‘ | ||
|
||
μμ μ¬λ‘μμ `try...catch` ꡬ쑰λ λΉλκΈ° μ½λμμ μ€λ₯λ₯Ό ν¬μ°©ν μ μλ€λ κ²μ μ΄ν΄ν΄μΌ ν©λλ€. | ||
|
||
λΉλκΈ° μ°μ°μ λ€λ£° λλ Promiseμμ `.catch` λ©μλλ₯Ό μ¬μ©νκ±°λ `async/await` ꡬ쑰 λ΄μμ `try...catch`λ₯Ό μ¬μ©νμ¬ μ€λ₯λ₯Ό μ²λ¦¬νλ λ± μ μ ν μ€λ₯ μ²λ¦¬ λ©μ»€λμ¦μ μ±ννλ κ²μ΄ μ€μν©λλ€. | ||
|
||
μ΄ μ κ·Ό λ°©μμ λΉλκΈ° μμ μ€μ λ°μν μ μλ μμΈλ₯Ό ν¨κ³Όμ μΌλ‘ ν¬μ°©νκ³ μ²λ¦¬ν λΏλ§ μλλΌ μ½λμ κ²¬κ³ μ±κ³Ό μ μ§λ³΄μμ±μ 보μ₯ν©λλ€. | ||
|
||
μΌμμ μΈ κ°λ° μμ μμ μλ°μ€ν¬λ¦½νΈ λ°νμ λ©μ»€λμ¦κ³Ό λΉλκΈ° νλ‘κ·Έλλ° λͺ¨λΈμ λν μ΄ν΄λ κ°μΈμ μ»€λ¦¬μ΄ μ±μ₯μ μ€μν μν μ νλ―λ‘ μμμ μΌλ‘ νꡬν΄μΌ ν©λλ€. |