๐Ÿ’€ Exception

Checked Exception / UnChecked Exception

Checked Exception UnChecked Exception
์—„๊ฒฉํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๊ณผ์ • ๊ฐ•์ œํ•˜์ง€ ์•Š๋Š” ์ฒ˜๋ฆฌ๊ณผ์ •
์ปดํŒŒ์ผ ๋‹จ๊ณ„ ์‹คํ–‰๋‹จ๊ณ„
roll-back โŒ roll-back โญ•
RuntimeException์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์˜ˆ์™ธ RuntimeException ์˜ ํ•˜์œ„ ์˜ˆ์™ธ

Service์™€ Exception

๋Œ€๊ฐœ Service๊ณ„์ธต์—์„œ๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ณธ ๋ชฉ์ ์— ๋งž๊ฒŒ Exception์ฒ˜๋ฆฌ๋Š” ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด ๋ง์€ ํ•„์ˆ˜์ ์ธ ๊ฒƒ ๊ณผ๋Š” ๋‹ค๋ฅธ ์˜๋ฏธ๋กœ์จ Exception์ฒ˜๋ฆฌ์˜ ํ•„์š”์„ฑ์ด ์—†๋‹ค๊ณ  ์ดํ•ดํ•˜๊ณ  ๋„˜์–ด๊ฐ€์ž.

โ“ ๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ• ๊นŒ

Service์™€ Controller ๊ณ„์ธต์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฒƒ์ด ์•„๋‹Œ DAO์—์„œ Exception์ฒ˜๋ฆฌ๊ฐ€ ์ด๋ฃจ์–ด์ง„๋‹ค. ์˜ˆ์ œ์˜ UserDAO์— SQLException์€ ์ฒ˜๋ฆฌ๋Š” Exception์„ ์ƒ์†๋ฐ›์•„ ์ง์ ‘ ์ •์˜ํ•œ UserDAOException ํด๋ž˜์Šค์—์„œ RuntimeException์„ ์‚ฌ์šฉํ•ด ์ „ํ™˜ํ•˜๋Š”๊ฒƒ์œผ๋กœ ์ด๋ฃจ์–ด์กŒ๋‹ค.

try{
		~
} catch (SQLException e) {
   throw new UserDAOException();
	 //์›๋ž˜๋Š” RuntimeException.
}

์ด์ฒ˜๋Ÿผ @ExceptionHandler Annotation์„ ์‚ฌ์šฉํ•œ Exception ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘์€ ๊ฐœ๋ณ„์ ์ธ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋ณด๋‹ค

  • ๋ชจ๋“  Exception์„ ํ•œ ๋ฉ”์„œ๋“œ์—์„œ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ 
  • ํ•ด๋‹น ํผ๋ฅผ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฐ™์€ ์ข…๋ฅ˜์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋กœ ์ธํ•œ ๋ณต๊ตฌ์˜ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง„๋‹ค๋Š”์ 
  • ๊ฐ๊ฐ์˜ ๋‹ค๋ฅธ ์˜ˆ์™ธ์ƒํ™ฉ๋“ค์— ๊ด€ํ•ด ๊ฐ๊ธฐ ๋‹ค๋ฅธ ๋ทฐ๋ฅผ ๋ฐ˜ํ™˜ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.