Skip to content

Latest commit

ย 

History

History
173 lines (106 loc) ยท 7.35 KB

Error & Exception.md

File metadata and controls

173 lines (106 loc) ยท 7.35 KB

Error & Exception

Assembled by GimunLee (2019-11-19)


Goal

  • Error์™€ Exception์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Exception Handling์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Abstract

Error์™€ Exception์€ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์‚ฌ์‹ค ํฐ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

Error ๋Š” ์ปดํŒŒ์ผ ์‹œ ๋ฌธ๋ฒ•์ ์ธ ์˜ค๋ฅ˜์™€ ๋Ÿฐํƒ€์ž„ ์‹œ ๋„ํฌ์ธํŠธ ์ฐธ์กฐ์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๋กœ ํ”„๋กœ์„ธ์Šค์— ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ๋ฅผ ์•ผ๊ธฐ ์‹œ์ผœ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Exception ์€ ์ปดํ“จํ„ฐ ์‹œ์Šคํ…œ์˜ ๋™์ž‘ ๋„์ค‘ ์˜ˆ๊ธฐ์น˜ ์•Š์•˜๋˜ ์ด์ƒ ์ƒํƒœ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์ˆ˜ํ–‰ ์ค‘์ธ ํ”„๋กœ๊ทธ๋žจ์ด ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๊ฒƒ์šฐ๋กœ ์˜ˆ๋ฅผ ๋“ค๋ฉด, ์—ฐ์‚ฐ ๋„์ค‘ ๋„˜์นจ์— ์˜ํ•ด ๋ฐœ์ƒํ•œ ๋ผ์–ด๋“ค๊ธฐ ๋“ฑ์ด ์ด์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰ ์ค‘ ์–ด๋–ค ์›์ธ์— ์˜ํ•ด์„œ ์˜ค์ž‘๋™์„ ํ•˜๊ฑฐ๋‚˜ ๋น„์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ํ”„๋กœ๊ทธ๋žจ ์˜ค๋ฅ˜๋ผ ํ•˜๊ณ , ํ”„๋กœ๊ทธ๋žจ ์˜ค๋ฅ˜์—๋Š” ์—๋Ÿฌ(error)์™€ ์˜ˆ์™ธ(exception) ๋‘ ๊ฐ€์ง€๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—๋Ÿฌ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ์ด๋‚˜ ์Šคํƒ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ์™€ ๊ฐ™์ด ๋ฐœ์ƒํ•˜๋ฉด ๋ณต๊ตฌํ•  ์ˆ˜ ์—†๋Š” ์‹ฌ๊ฐํ•œ ์˜ค๋ฅ˜์ด๊ณ , ์˜ˆ์™ธ๋Š” ๋ฐœ์ƒํ•˜๋”๋ผ๋„ ์ˆ˜์Šตํ•  ์ˆ˜ ์žˆ๋Š” ๋น„๊ต์  ๋œ ์‹ฌ๊ฐํ•œ ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค. ์ด ์˜ˆ์™ธ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ ์ ˆํžˆ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋น„์ •์ƒ์ ์ธ ์ข…๋ฅ˜๋ฅผ ๋ง‰์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Error์˜ ์ƒํ™ฉ์„ ๋ฏธ๋ฆฌ ๋ฏธ์—ฐ์— ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ Exception ์ƒํ™ฉ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฉฐ, java์—์„œ๋Š” try-catch๋ฌธ์œผ๋กœ Exception handling์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


Exception Handling

์ž˜๋ชป๋œ ํ•˜๋‚˜๋กœ ์ธํ•ด ์ „์ฒด ์‹œ์Šคํ…œ์ด ๋ฌด๋„ˆ์ง€๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ์ˆ ์ ์ธ ์ฒ˜๋ฆฌ์ž…๋‹ˆ๋‹ค. JAVA์—์„œ๋Š” ์˜ˆ์™ธ์™€ ์—๋Ÿฌ๋„ ๊ฐ์ฒด๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์™ธ๊ฐ€ ์ฃผ๋กœ ๋ฐœ์ƒํ•˜๋Š” ์›์ธ

  • ์‚ฌ์šฉ์ž์˜ ์ž˜๋ชป๋œ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ
  • ์ž˜๋ชป๋œ ์—ฐ์‚ฐ
  • ๊ฐœ๋ฐœ์ž๊ฐ€ ๋กœ์ง์„ ์ž˜๋ชป ์ž‘์„ฑ
  • ํ•˜๋“œ์›จ์–ด, ๋„คํŠธ์›Œํฌ ์˜ค์ž‘๋™
  • ์‹œ์Šคํ…œ ๊ณผ๋ถ€ํ•˜

Throwable ํด๋ž˜์Šค

Throwable ํด๋ž˜์Šค๋Š” ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์ƒ์œ„ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. Exception๊ณผ Error๋Š” Throwable์˜ ์ƒ์†์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.


Error (์—๋Ÿฌ)

Error๋Š” ์‹œ์Šคํ…œ ๋ ˆ๋ฒจ์—์„œ ๋ฐœ์ƒํ•˜์—ฌ, ๊ฐœ๋ฐœ์ž๊ฐ€ ์–ด๋–ป๊ฒŒ ์กฐ์น˜ํ•  ์ˆ˜ ์—†๋Š” ์ˆ˜์ค€์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

  • OutOfMemoryError : JVM์— ์„ค์ •๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ํ•œ๊ณ„๋ฅผ ๋ฒ—์–ด๋‚œ ์ƒํ™ฉ์ผ ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ํž™ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋ถ€์กฑํ•˜๊ฑฐ๋‚˜, ๋„ˆ๋ฌด ๋งŽ์€ class๋ฅผ ๋กœ๋“œํ• ๋•Œ, ๊ฐ€์šฉ๊ฐ€๋Šฅํ•œ swap์ด ์—†์„๋•Œ, ํฐ ๋ฉ”๋ชจ๋ฆฌ์˜ native๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ์œ„ํ•ด dump ํŒŒ์ผ๋ถ„์„, jvm ์˜ต์…˜ ์ˆ˜์ • ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Exception (์˜ˆ์™ธ)

์˜ˆ์™ธ๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๊ตฌํ˜„ํ•œ ๋กœ์ง์—์„œ ๋ฐœ์ƒํ•˜๋ฉฐ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€๋Šฅํ•œ ๊ฒƒ๋“ค๋กœ JVM์€ ์ •์ƒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.


Exception์˜ 2๊ฐ€์ง€ ์ข…๋ฅ˜

  1. Checked Exception : ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์ˆ˜์ด๋ฉฐ, ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. JVM ์™ธ๋ถ€์™€ ํ†ต์‹ (๋„คํŠธ์›Œํฌ, ํŒŒ์ผ์‹œ์Šคํ…œ ๋“ฑ)ํ•  ๋•Œ ์ฃผ๋กœ ์“ฐ์ž…๋‹ˆ๋‹ค.

    • RuntimeException ์ด์™ธ์— ์žˆ๋Š” ๋ชจ๋“  ์˜ˆ์™ธ
    • IOException, SQLException ๋“ฑ
  2. Unchecked Exception : ์ปดํŒŒ์ผ ๋•Œ ์ฒดํฌ๋˜์ง€ ์•Š๊ณ , Runtime์— ๋ฐœ์ƒํ•˜๋Š” Exception์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

    • RuntimeException ํ•˜์œ„์˜ ๋ชจ๋“  ์˜ˆ์™ธ
    • NullPointerException, IndexOutOfBoundException ๋“ฑ

๋Œ€ํ‘œ์ ์ธ Exception Class

  • NullPointerException : Null ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ฐธ์กฐํ• ๋•Œ ๋ฐœ์ƒ, ๋ญ”๊ฐ€ ๋™์ž‘์‹œํ‚ฌ ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  • IndexOutOfBoundsException : ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌํ•œ ์ž๋ฃŒ๊ตฌ์กฐ(๋ฌธ์ž์—ด, ๋ฐฐ์—ด, ์ž๋ฃŒ๊ตฌ์กฐ)์—์„œ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ ์‚ฌ์šฉ์œผ๋กœ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  • FormatException : ๋ฌธ์ž์—ด, ์ˆซ์ž, ๋‚ ์งœ ๋ณ€ํ™˜ ์‹œ ์ž˜๋ชป๋œ ๋ฐ์ดํ„ฐ(ex. "123A" -> 123 ์œผ๋กœ ๋ณ€ํ™˜ ์‹œ)๋กœ ๋ฐœ์ƒํ•˜๋ฉฐ, ๋ณดํ†ต ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ, ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ, ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ์˜ ๋ณ€ํ™˜ ์ฒ˜๋ฆฌ์—์„œ ์ž์ฃผ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  • ArthmeticException : ์ •์ˆ˜๋ฅผ 0์œผ๋กœ ๋‚˜๋ˆŒ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  • ClassCastException : ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†๋Š” ํƒ€์ž…์œผ๋กœ ๊ฐ์ฒด๋ฅผ ๋ณ€ํ™˜ํ•  ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  • IllegalArgumentException : ์ž˜๋ชป๋œ ์ธ์ž ์ „๋‹ฌ ์‹œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  • IOException : ์ž…์ถœ๋ ฅ ๋™์ž‘ ์‹คํŒจ ๋˜๋Š” ์ธํ„ฐ๋ŸฝํŠธ ์‹œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  • IllegalStateException : ๊ฐ์ฒด์˜ ์ƒํƒœ๊ฐ€ ๋งค์†Œ๋“œ ํ˜ธ์ถœ์—๋Š” ๋ถ€์ ์ ˆํ•œ ๊ฒฝ์šฐ์— ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  • ConcurrentModificationException : ๊ธˆ์ง€๋œ ๊ณณ์—์„œ ๊ฐ์ฒด๋ฅผ ๋™์‹œ์— ์ˆ˜์ •ํ•˜๋Š”๊ฒƒ์ด ๊ฐ์ง€๋  ๊ฒฝ์šฐ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  • UnsupportedOperationException : ๊ฐ์ฒด๊ฐ€ ๋ฉ”์†Œ๋“œ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์š” Method

  • printStackTrace() : ๋ฐœ์ƒํ•œ Exception์˜ ์ถœ์ฒ˜๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์ƒ์—์„œ ์ถ”์ ํ•˜๋ฉด์„œ ๊ฒฐ๊ณผ๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๋ฐœ์ƒํ•œ ์œ„์น˜๋ฅผ ์ •ํ™•ํžˆ ์ถœ๋ ฅํ•ด์ค˜์„œ ์ œ์ผ ๋งŽ์ด ์“ฐ๋ฉฐ void๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • getMessage() : ํ•œ์ค„๋กœ ์š”์•ฝ๋œ ๋ฉ”์„ธ์ง€๋ฅผ String์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.
  • getStackTrace() : jdk1.4 ๋ถ€ํ„ฐ ์ง€์›, printStackTrace()๋ฅผ ๋ณด์™„, StackTraceElement[] ์ด๋ผ๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ๋ณ€๊ฒฝํ•ด์„œ ์ถœ๋ ฅํ•˜๊ณ  ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

Exception Handling

JAVA์—์„œ ๋ชจ๋“  ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด (XXX)Exception ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ํฌ๊ฒŒ 2๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ์ง์ ‘ try ~ catch ๋ฅผ ์ด์šฉํ•ด์„œ ์˜ˆ์™ธ์— ๋Œ€ํ•œ ์ตœ์ข…์ ์ธ ์ฑ…์ž„์„ ์ง€๊ณ  ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹
  2. throws Exception ์„ ์ด์šฉํ•ด์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ์˜ ์ฑ…์ž„์„ ํ˜ธ์ถœํ•˜๋Š” ์ชฝ์ด ์ฑ…์ž„์ง€๋„๋ก ํ•˜๋Š” ๋ฐฉ์‹ (์ฃผ๋กœ ํ˜ธ์ถœํ•˜๋Š” ์ชฝ์— ์˜ˆ์™ธ๋ฅผ ๋ณด๊ณ ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.)

๋‹ค๋ฅธ ๋ฉ”์†Œ๋“œ์˜ ์ผ๋ถ€๋ถ„์œผ๋กœ ๋™์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ์—” ๋˜์ง€๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค.


์˜ˆ์™ธ ์žก๊ธฐ (try ~ catch ๊ตฌ๋ฌธ)

๋กœ์ง ์ค‘์— ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ• ์ง€๋„ ๋ชจ๋ฅด๋Š” ๋ถ€๋ถ„์— try ~ catch ๊ตฌ๋ฌธ์œผ๋กœ ๋ณดํ—˜ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

  • try ์—๋Š” ์œ„ํ—˜ํ•œ ๋กœ์ง์ด ๋“ค์–ด๊ฐ€๊ณ , catch ์—๋Š” ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ์ˆ˜ํ–‰ํ•  ๋กœ์ง์ด ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.
  • try ์ค‘์ด๋ผ๋„ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ ๋‹ค์Œ์˜ ์ฝ”๋“œ๋“ค์€ ์‹คํ–‰๋˜์ง€ ์•Š์œผ๋ฉฐ catch ๊ตฌ๋ฌธ์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.
  • catch ๊ตฌ๋ฌธ์€ else if ์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ๊ฐœ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • finally ๋Š” ๋งˆ์ง€๋ง‰์— ์‹คํ–‰ํ•˜๊ณ  ์‹ถ์€ ๋กœ์ง์ด ๋“ค์–ด๊ฐ€๋ฉฐ, ๋Œ€ํ‘œ์ ์œผ๋กœ .close() ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์™ธ ๋˜์ง€๊ธฐ (throws ๊ตฌ๋ฌธ)

์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ˜„์žฌ ๋ฉ”์†Œ๋“œ๊ฐ€ ์ง์ ‘ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ  ํ˜ธ์ถœํ•œ ๊ณณ์—๋‹ค๊ฐ€ ์˜ˆ์™ธ์˜ ๋ฐœ์ƒ ์—ฌ๋ถ€๋ฅผ ํ†ต๋ณดํ•ฉ๋‹ˆ๋‹ค. ํ˜ธ์ถœํ•œ ๋ฉ”์†Œ๋“œ๋Š” ์ด๊ฑธ ๋˜ ๋˜์งˆ๊ฑด์ง€ ์ง์ ‘ ์ฒ˜๋ฆฌํ•  ๊ฑด์ง€ ์ •ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. (return๋ณด๋‹ค ๊ฐ•๋ ฅํ•ฉ๋‹ˆ๋‹ค.)

public class ThrowsEx {
    public void call_A() throws Exception {
        call_B();
    }

    private void call_B() throws Exception {
        call_C();
    }

    private void call_C() throws Exception {
        System.out.println(1 / 0);
    }

    public static void main(String[] args) throws Exception {
        ThrowsEx test = new ThrowsEx();
        test.call_A();
    }
}

์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at exception.ThrowsEx.call_C(ThrowsEx.java:13)
    at exception.ThrowsEx.call_B(ThrowsEx.java:9)
    at exception.ThrowsEx.call_A(ThrowsEx.java:5)
    at exception.ThrowsEx.main(ThrowsEx.java:18)

Reference & Additional Resources