๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš— Major Study (Bachelor)/๐ŸŸ  JAVA Programming

JAVA ํ”„๋กœ๊ทธ๋ž˜๋ฐ 9.1

by UKHYUN22 2021. 11. 2.
728x90

Exception Handling

 

 

normal operation, exceptional case

 

Normal case์™€ Exceptional case๋กœ ๋‚˜๋‰˜๊ณ 

Exceptional case๋Š” ํฌ๊ฒŒ ๋‘ ๊ฐœ ๋ฐœ์ƒํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ์˜ˆ์™ธ์ƒํ™ฉ ๋‘ ๋ฒˆ์งธ๋Š” ์—๋Ÿฌ์ด๋‹ค.

ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ•  ๋•Œ incremently(์ ์ฐจ์ ์œผ๋กœ) ํ•˜๊ฒŒ ๊ฐœ๋ฐœ์„ ํ•œ๋‹ค.

์ผ๋‹จ normal coding์„ ํ•˜๊ณ  exceptional case๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด incremently ์ฝ”๋”ฉ์ด๋‹ค.

์‰ฝ๊ฒŒ ๋ถ„๋ฆฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ด ์šฐ๋ฆฌ๊ฐ€ ๋‹ค๋ฃจ๊ณ ์ž ํ•˜๋Š” Exceptional ์ฝ”๋”ฉ์ด ๋œ๋‹ค. (isolated)

 

 

 

Throwing, Handling, catching

 

Throwing - exception์„ ๋ฐœ์ƒํ•œ๋‹ค๋ผ๊ณ  ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

Handling์„ ํ•  ๋•Œ ์ค‘์š”ํ•œ ๊ฒƒ์€ catching ์ด๋‹ค. Exception์ด Throw๋˜๋ฉด  ์–ด๋–ค Exception์ธ์ง€ ํ™•์ธํ•˜๊ณ  ๊ทธ์— ๋”ฐ๋ฅธ ์ ์ ˆํ•œ ๋Œ€์‘์ฑ…์„ handling์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

 

 

๊ฐ€์žฅ ๋Œ€์ค‘์ ์ธ ๋ฐฉ๋ฒ•์ธ try-throw-catch ๋ฌธ์ด๋‹ค.

new Exception์œผ๋กœ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

catch ๋ฌธ์„ ํ†ตํ•ด์„œ Exception์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค.

 

try block, catch block, Exception

 

catch block์€ ์ผ์ข…์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›๋Š”๋ฐ Exception ํƒ€์ž…์œผ๋กœ ๋ฐ›๊ฒŒ ๋œ๋‹ค.

 

 

not, getMessage

 

catch block์€ ๋ฉ”์†Œ๋“œ๊ฐ€ ์•„๋‹ˆ๋‹ค. catch๋ฌธ์˜ ๋ฌธ๋ฒ•์ ์ธ ํ•œ ๋ถ€๋ถ„์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ชจ๋“  Exception์€ getMessage๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค.

 

 

object

 

Exception์€ Object์ด๋‹ค. Exception์ด๋ผ๋Š” ์˜ค๋ธŒ์ ํŠธ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ ๋˜์กŒ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๋ชจ๋“  exceptions๋“ค์€ Exception ํด๋ž˜์Šค์˜ child class์— ํ•ด๋‹นํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ๋œ๋‹ค.

์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ๋งŒ๋“ค ์ˆ˜ ์žˆ์ง€๋งŒ JAVA๊ฐ€ ๋ฏธ๋ฆฌ ์šฐ๋ฆฌ์—๊ฒŒ ์ œ๊ณตํ•ด์ฃผ๊ณ  ์žˆ๋Š” Exception์ด ์žˆ๋‹ค.