Skip to content

Commit

Permalink
Merge pull request #330 from Flamme1004K/item69
Browse files Browse the repository at this point in the history
[#69][2κΈ°] μ˜ˆμ™ΈλŠ” μ§„μ§œ μ˜ˆμ™Έ μƒν™©μ—λ§Œ μ‚¬μš©ν•˜λΌ
  • Loading branch information
ksy90101 authored Oct 30, 2021
2 parents aa02fc7 + 4b5b545 commit 4424c39
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Item69 μ˜ˆμ™ΈλŠ” μ§„μ§œ μ˜ˆμ™Έ μƒν™©μ—λ§Œ μ‚¬μš©ν•˜λΌ

try {
int i = 0;
while(true) {
range[i++].climb()
}
} catch (ArrayIndexOutOfBoundsException e) {
}

ν•΄λ‹Ή μ½”λ“œμ˜ λ¬Έμ œμ μ€ λ¬΄μ—‡μΌκΉŒ?
- ν•΄λ‹Ή μ½”λ“œλŠ” λ¬΄μŠ¨μΌμ„ ν•˜λŠ” μ½”λ“œμΈμ§€ μ•Œ μˆ˜κ°€ μ—†λ‹€.
- λ¬΄ν•œλ£¨ν”„λ₯Ό λŒλ‹€κ°€ λ°°μ—΄μ˜ 끝에 도달해 ArrayIndexOutOfBoundsException이 λ°œμƒν•΄μ•Ό λμ΄λ‚œλ‹€.

> 첫 번째 μ˜ˆμ‹œμ—μ„œλŠ” μ™œ Exception을 λ˜μ Έμ€¬μ„κΉŒ? λ°”λ‘œ JVMμ—μ„œ 배열에 μ ‘κ·Όν•  λ•Œλ§ˆλ‹€ 경계λ₯Ό λ„˜μ§€ μ•ŠλŠ”μ§€ κ²€μ‚¬ν•˜λŠ”λ°, 일반적인 λ°˜λ³΅λ¬Έλ„ λ°°μ—΄ 경계에 λ„λ‹¬ν•˜λ©΄ μ’…λ£Œλœλ‹€λŠ” 희망에 λ˜μ Έμ‘Œμ„ 것이라고, μ±…μ—μ„œ 말해쀀닀.
> ν•˜μ§€λ§Œ, μ±…μ—μ„œλŠ” μ„Έκ°€μ§€λ©΄μ—μ„œ 잘λͺ» 된 좔둠이라고 ν•œλ‹€.
> 1. μ˜ˆμ™ΈλŠ” μ˜ˆμ™Έ 상황에 μ“Έ μš©λ„λ‘œ μ„€κ³„λ˜μ—ˆμœΌλ―€λ‘œ μ˜ˆμ™Έμ—μ„œλŠ” μ΅œμ ν™”μ— λ³„λ‘œ μ‹ κ²½ 쓰지 μ•Šμ•˜μ„ κ°€λŠ₯성이 크닀.
> 2. μ½”λ“œλ₯Ό try-catch블둝 μ•ˆμ— λ„£μœΌλ©΄ JVM이 μ μš©ν•  수 μžˆλŠ” μ΅œμ ν™”κ°€ μ œν•œ λœλ‹€.
> 3. 배열을 μˆœνšŒν•˜λŠ” ν‘œμ€€ κ΄€μš©κ΅¬λŠ” μ•žμ„œ κ±±μ •ν•œ 쀑볡 검사λ₯Ό μˆ˜ν–‰ν•˜μ§€ μ•ŠλŠ”λ‹€. JVM이 μ•Œμ•„μ„œ μ΅œμ ν™”ν•΄ μ—†μ• μ€€λ‹€.

ν•΄λ‹Ή μ½”λ“œλ₯Ό λ‹€μ‹œ λ¦¬νŒ©ν† λ§ ν•œλ‹€λ©΄,

for (Mountain m : range) {
m.climb();
}

μ•„λž˜μ™€ 같은 μž₯점을 κ°€μ§€κ²Œ λœλ‹€.
- λ¬΄μŠ¨μΌμ„ ν•˜λŠ” μ½”λ“œμΈμ§€ μ§κ΄€μ μ΄κ²Œ 확인할 수 μžˆλ‹€.
- forLoopκ°€ λλ‚˜λ©΄ ν•΄λ‹Ή μ½”λ“œλŠ” λλ‚˜κΈ° λ•Œλ¬Έμ— λΆˆν•„μš”ν•œ Exception을 던질 ν•„μš”κ°€ μ—†λ‹€.

μš”λ²ˆ μ•„μ΄ν…œμ€ λ°”λ‘œ μž₯점 쀑에 λ‘λ²ˆμ§Έμ— ν•΄λ‹Ήν•˜λŠ” Exception에 μ˜¬λ°”λ₯Έ μ“°μž„μ— λŒ€ν•΄ 2가지 ν•­λͺ©μœΌλ‘œ μ •λ¦¬ν•˜λ©° 끝내고 μžˆλ‹€.

1. μ˜ˆμ™ΈλŠ” 였직 μ˜ˆμ™Έ μƒν™©μ—μ„œλ§Œ 써야 ν•œλ‹€. μ ˆλŒ€λ‘œ 첫번째 μ˜ˆμ‹œμ²˜λŸΌ, 일상적인 μ œμ–΄ νλ¦„μš©μœΌλ‘œ μ“°μ—¬μ„  μ•ˆλœλ‹€.
2. 잘 μ„€κ³„λœ API라면 ν΄λΌμ΄μ–ΈνŠΈκ°€ 정상적인 μ œμ–΄ νλ¦„μ—μ„œ μ˜ˆμ™Έλ₯Ό μ‚¬μš©ν•  일이 μ—†κ²Œ ν•΄μ•Όν•œλ‹€.

0 comments on commit 4424c39

Please sign in to comment.