All :L

[Java] ์˜ˆ์™ธ (Exception) ๋ณธ๋ฌธ

STUDY/Java

[Java] ์˜ˆ์™ธ (Exception)

ofijwe 2024. 11. 1. 18:02
๋ฐ˜์‘ํ˜•

๐Ÿ“’ ์˜ˆ์™ธ์˜ ์ฒ˜๋ฆฌ

1๏ธโƒฃ ์—๋Ÿฌ์™€ ์˜ˆ์™ธ

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

2๏ธโƒฃ ์˜ˆ์™ธ ํด๋ž˜์Šค์˜ ๊ณ„์ธต

  • checked exception → ์˜ˆ์™ธ์— ๋Œ€ํ•œ ๋Œ€์ฒ˜ ์ฝ”๋“œ๊ฐ€ ์—†์œผ๋ฉด ์ปดํŒŒ์ผ์ด ์ง„ํ–‰๋˜์ง€ X
  • unchecked exception → ์˜ˆ์™ธ์—๋Œ€ํ•œ ์ฝ”๋“œ๊ฐ€ ์—†๋”๋ผ๋„ ์ปดํŒŒ์ผ์€ ์ง„ํ–‰๋จ

3๏ธโƒฃ Exception handling ๊ธฐ๋ฒ•

  • try{} catch() {}
  • public class SimpleException { public static void main(String[] args) { int[] intArray = { 10 }; try { System.out.println(intArray[2]); } catch (ArrayIndexOutOfBoundsException e) { // ์‚ฌ๊ณ ์ฒ˜๋ฆฌ System.out.println(e.getMessage()); e.printStackTrace(); } System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค."); } }
  • Throwable์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ
    • public String getMessage() - ๋ฐœ์ƒ๋œ ์˜ˆ์™ธ์— ๋Œ€ํ•œ ๊ตฌ์ฒด์ ์ธ ๋ฉ”์‹œ์ง€ ๋ฐ˜ํ™˜
    • public Throwable getCause() - ์˜ˆ์™ธ์˜ ์›์ธ์ด ๋˜๋Š” Throwable ๊ฐ์ฒด ๋˜๋Š” null์„ ๋ฐ˜ํ™˜
    • public void printStackTrace() - ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒ๋œ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๊ธฐ๊นŒ์ง€์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์Šคํƒ์„ ์ถœ๋ ฅ / ์ฃผ๋กœ ๋””๋ฒ„๊น…์˜ ์ˆ˜๋‹จ์œผ๋กœ ์‚ฌ์šฉ

4๏ธโƒฃ try-catch๋ฌธ ํ๋ฆ„

  • try-catch๋ฌธ ํ๋ฆ„
    • try ๋ธ”๋ก์—์„œ ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ JVM์ด ํ•ด๋‹น Exception ํด๋ž˜์Šค์˜ ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ ๋˜์ง(throw)
    • ๋˜์ ธ์ง„ exception์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” catch ๋ธ”๋ก์—์„œ ๋ฐ›์€ ํ›„ ์ฒ˜๋ฆฌ
    • ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๋ฉด try-catch๋ธ”๋ก์„ ๋ฒ—์–ด๋‚˜ ๋‹ค์Œ ๋ฌธ์žฅ ์ง„ํ–‰
  • try ๋ธ”๋ก์—์„œ ์–ด๋– ํ•œ ์˜ˆ์™ธ๋„ ๋ฐœ์ƒํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ
    • catch ๋ฌธ์„ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  try-catch๋ธ”๋ก์˜ ๋‹ค์Œ ํ๋ฆ„ ๋ฌธ์žฅ ์‹คํ–‰
  • Checked Exception ์ฒ˜๋ฆฌ
    • ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ ๋ถˆ๊ฐ€ : Checked Exception
  • public class CheckedExceptionHandling { public static void main(String[] args) { // TODO: ๋‹ค์Œ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•ด๋ณด์ž. // ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด try-catch๋ฌธ ์‚ฌ์šฉ try { Class<?> myclass = Class.forName("com.ssafy.day09.a_basic.SimpleException"); // Unhandled exception type ClassNotFoundException ๋ผ๋Š” ์—๋Ÿฌ ๋ฐœ์ƒ System.out.println(myclass.getName()); } catch (ClassNotFoundException e) { e.printStackTrace(); } // END System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ •์ƒ ์ข…๋ฃŒ"); } }

5๏ธโƒฃ ๋‹ค์ค‘ exceptino handling

  • try ๋ธ”๋ก์—์„œ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ
    • ํ•˜๋‚˜์˜ try ๋ธ”๋ก์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ catch ๋ธ”๋ก ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
    • ์˜ˆ์™ธ ์ข…๋ฅ˜๋ณ„๋กœ catch ๋ธ”๋ก ๊ตฌ์„ฑ
    • ์ฒ˜๋ฆฌ๋  catch ๋ฌธ์žฅ์„ ์ฐพ์„ ๋•Œ๋Š” ๋‹คํ˜•์„ฑ์ด ์ ์šฉ๋จ
  • ๋‹ค์ค‘ catch ๋ฌธ์žฅ ์ž‘์„ฑ ์ˆœ์„ฑ ์œ ์˜ ์‚ฌํ•ญ
    • ์ƒ์œ„ ํƒ€์ž…์˜ ์˜ˆ์™ธ๊ฐ€ ๋จผ์ € ์„ ์–ธ๋˜๋Š” ๊ฒฝ์šฐ ๋’ค์— ๋“ฑ์žฅํ•˜๋Š” catch ๋ธ”๋ก ๋™์ž‘ ๊ธฐํšŒ X
      • Unreachable catch block for Exception
      • ์ƒ์† ๊ด€๊ณ„๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋Š” ๋ฌด๊ด€
    • ์ƒ์† ๊ด€๊ณ„์—์„œ๋Š” ์ž‘์€ ๋ฒ”์œ„(์ž์‹)์—์„œ ํฐ ๋ฒ”์œ„(์กฐ์ƒ)์ˆœ์œผ๋กœ ์ •์˜
  • FileNotFoundException์˜ ์ƒ์† ๊ด€๊ณ„
    • java.lang.Object → java.lang.Throwable → java.lang.Exception → java.lang.IOException → java.lang.FileNotFoundException

๐Ÿ“’ try ~ catch ~ finally

1๏ธโƒฃ try ~ catch ~ finally ๊ตฌ๋ฌธ์„ ์ด์šฉํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

  • finally๋Š” ์˜ˆ์™ธ ๋ฐœ์ƒ ์—ฌ๋ถ€์™€ ์ƒ๊ด€์—†์ด ์–ธ์ œ๋‚˜ ์‹คํ–‰
    • ์ค‘๊ฐ„์— return์„ ๋งŒ๋‚˜๋Š” ๊ฒฝ์šฐ๋„ finally ๋ธ”๋ก์„ ๋จผ์ € ์ˆ˜ํ–‰ ํ›„ ๋ฆฌํ„ด ์‹คํ–‰
  • ์ฃผ์š” ๋ชฉ์  → try ๋ธ”๋ก์—์„œ ์‚ฌ์šฉํ•œ ๋ฆฌ์†Œ์Šค ๋ฐ˜๋‚ฉ
  • ์ƒ์„ฑํ•œ ์‹œ์Šคํ…œ ์ž์› ๋ฐ˜๋‚ฉ ํ•„์š”
    • ๋ฐ˜๋‚ฉํ•˜์ง€ ์•Š์„ ์‹œ ์žฅ๋ž˜ resource leak ๋ฐœ์ƒ ๊ฐ€๋Šฅ → ๋ฐ˜๋“œ์‹œ close ์ฒ˜๋ฆฌ
  • ์ง€์ €๋ถ„ํ•  ์ˆ˜ ๋ฐ–์— ์—†๋Š” finally ๋ธ”๋ก
    • close ๋ฉ”์„œ๋“œ ์ž์ฒด๊ฐ€ IOException ์œ ๋ฐœ ๊ฐ€๋Šฅ
    • FileInputStream ์ƒ์„ฑ์ž์—์„œ IOException ๋ฐœ์ƒ ์‹œ fileInput์€ null์ธ ์ƒํ™ฉ

2๏ธโƒฃ try ~ with ~resources

  • ๋ฆฌ์†Œ์Šค์˜ ์ž๋™ close ์ฒ˜๋ฆฌ
    • try(๋ฆฌ์†Œ์Šค_ํƒ€์ž…1 res1 = ์ดˆ๊ธฐํ™”; ๋ฆฌ์†Œ์Šค_ํƒ€์ž…2 res2 = ์ดˆ๊ธฐํ™”; …) {} catch() {}
  • try ์„ ์–ธ๋ฌธ์— ์„ ์–ธ๋œ ๊ฐ์ฒด๋“ค์— ๋Œ€ํ•ด ์ž๋™ close ํ˜ธ์ถœ(finally ์—ญํ• )
    • ํ•ด๋‹น ๊ฐ์ฒด๋“ค์ด AutoCloseable interface๋ฅผ ๊ตฌํ˜„ํ•  ๊ฒƒ
    • ํ•ด๋‹น ๊ฐ์ฒด๋Š” try ๋ธ”๋ก์—์„œ ๋‹ค์‹œ ํ• ๋‹น๋  ์ˆ˜ X → final ๋ณ€์ˆ˜๋กœ ๊ฐ„์ฃผ
  • ์ฃผ์˜์ 
    • ํ™•์žฅ๋œ rtywithresources ์‚ฌ์šฉ์‹œ ๋ฌธ์žฅ์— ํ•˜๋‚˜ ์ด์ƒ์˜ catch ๋˜๋Š” finally ํ•„์š”
    • ์ž๋™ ์ƒ์„ฑ๋˜๋Š” ์ฝ”๋“œ๋“ค์ด ์‹ค์ œ๋กœ ์–ด๋–ป๊ฒŒ ๋™์ž‘๋˜๋Š”์ง€ ์ •ํ™•ํžˆ ์•Œ์•„์•ผ ํ•จ.
    • trywithresources๋ฌธ์žฅ์€ nested try ๋ธ”๋ก์„ ๊ตฌ์„ฑ
  • ๋‹จ์ˆœ VS ํ™•์žฅ trywithresources
    • ๋‹จ์ˆœ → ๊ทธ๋ƒฅ ์“ฐ๊ณ  close๋งŒ ํ•  ๊ฒฝ์šฐ ์ตœ์ 

๐Ÿ“’ throws ํ™œ์šฉ

1๏ธโƒฃ throws ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•œ ์ฒ˜๋ฆฌ ์œ„์ž„

  • method์—์„œ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•  ํ•˜๋‚˜ ์ด์ƒ์˜ ์˜ˆ์™ธ๋ฅผ ํ˜ธ์ถœํ•œ ๊ณณ์œผ๋กœ ์ „๋‹ฌ (์ฒ˜๋ฆฌ ์œ„์ž„)
    • ๋‹จ์ˆœ ์ „๋‹ฌ(์˜ˆ์™ธ ์—†์–ด์ง€๋Š” ๊ฒƒ X)
    • ์˜ˆ์™ธ ์ „๋‹ฌ๋ฐ›์€ ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์‹œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ์˜ ์ฑ…์ž„ ๋ฐœ์ƒ
    • ์ฒ˜๋ฆฌํ•˜๋ ค๋Š” ์˜ˆ์™ธ ์กฐ์ƒ ํƒ€์ž…์œผ๋กœ throws ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ
  • checked exception๊ณผ throws
    • checked exception → ๋ฐ˜๋“œ์‹œ try~catch or throws ํ•„์š”
    • ํ•„์š”ํ•œ ๊ณณ์—์„œ try~catch ์ฒ˜๋ฆฌ
  • runtime exception๊ณผ throws
    • runtime exception์€ throws ํ•˜์ง€ ์•Š์•„๋„ ์ „๋‹ฌ๋จ.
    • but, ๊ฒฐ๊ตญ try~catch๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•จ.
  • ๋กœ๊ทธ ๋ถ„์„๊ณผ ์˜ˆ์™ธ์˜ ์ถ”์ 
    • Throwable์˜ printStackTrace๋Š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์Šคํƒ ์ •๋ณด ์กฐํšŒ ๊ฐ€๋Šฅ
      • ์ตœ์ดˆ ํ˜ธ์ถœ ๋ฉ”์„œ๋“œ๋ถ€ํ„ฐ ์˜ˆ์™ธ ๋ฐœ์ƒ ๋ฉ”์„œ๋“œ๊นŒ์ง€ ์Šคํƒ ์ •๋ณด์ถœ๋ ฅ
    • ํ™•์ธํ•ด์•ผํ•  ์ •๋ณด
      • ์˜ˆ์™ธ ์ข…๋ฅ˜ →์–ด๋–ค ์˜ˆ์™ธ์ธ์ง€
      • ์˜ˆ์™ธ ์›์ธ → ์˜ˆ์™ธ ๊ฐ์ฒด์˜ ๋ฉ”์‹œ์ง€๋Š” ๋ฌด์—‡์ธ์ง€
      • ๋””๋ฒ„๊น… ์ถœ๋ฐœ์  → ์–ด๋””์„œ ๋ฐœ์ƒํ–ˆ๋Š”์ง€
  • throws์˜ ๋ชฉ์ ๊ณผ API ํ™œ์šฉ
    • API๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” ์‚ฌ์ „ ์˜ˆ์™ธ ๋ฐœ์ƒ ๊ฐ€๋Šฅ์„ ์„ ์–ธ๋ถ€์— ๋ช…์‹œ (์˜ˆ์™ธ ๋ฐœ์ƒ - ์˜ˆ์™ธ ์ „ํŒŒ)
    • ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๊ฐ•์š” (์ƒํ™ฉ ์ธ์ง€ - ์ ์ ˆํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ)

2๏ธโƒฃ ๋ฉ”์„œ๋“œ ์žฌ์ •์˜์™€ throws

  • ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋“œ 5๊ฐ€์ง€ role
    • ์ด๋ฆ„, ํŒŒ๋ผ๋ฏธํ„ฐ, ๋ฆฌํ„ดํƒ€์ž…, ์ ‘๊ทผ์ œํ•œ์ž, ์˜ˆ์™ธ์ฒ˜๋ฆฌ
  • ๋ฉ”์„œ๋“œ ์žฌ์ •์˜ ์‹œ ์กฐ์ƒ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๊ฐ€ ๋˜์ง€๋Š” ์˜ˆ์™ธ๋ณด๋‹ค ๋ถ€๋ชจ์˜ˆ์™ธ๋ฅผ ๋˜์งˆ ์ˆ˜ X
    • ์ฆ‰, ๋ถ€๋ชจ๊ฐ€ ์น˜์ง€ ์•Š์€ ์‚ฌ๊ณ ๋ฅผ ์ž์‹์ด ์น  ์ˆ˜ X

3๏ธโƒฃ ์˜ˆ์™ธ ๋ณ€ํ™˜

  • ํ•˜์œ„ ๊ณ„์ธต์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋Š” ์ƒ์œ„ ๊ณ„์ธต์— ๋งž๋Š” ์˜ˆ์™ธ๋กœ ๋ฐ”๊ฟ”์„œ ๋˜์ ธ์•ผ ํ•จ.
  • ์‰ฝ๊ฒŒ ๋งํ•ด, ๋ฐฐ์†ก ์ธก์—์„œ ๋ฐ›์€ ๋ฌธ์ œ์‚ฌํ•ญ์„ ์ถ”๊ฐ€๋กœ ๊ฐ€๊ณตํ•ด์ฃผ๊ณ  ์ƒˆ๋กœ ๊ฐ€๊ณต๋œ ๊ฑธ ๊ณ ๊ฐ์—๊ฒŒ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๊ฒƒ
  • Exception Chaining
    • ํ•˜์œ„ ๊ณ„์ธต์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ ์ •๋ณด๊ฐ€ ์ƒ์œ„ ๊ณ„์ธต์˜ ์˜ˆ์™ธ๋ฅผ ๋””๋ฒ„๊น…ํ•˜๋Š”๋ฐ ์œ ์šฉํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉ
    • ํ•˜์œ„ ๊ณ„์ธต์˜ ์˜ˆ์™ธ๋ฅผ ์›์ธ์œผ๋กœ ์ƒ์œ„ ๊ณ„์ธต์—์„œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ

๐Ÿ“’ ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ

1๏ธโƒฃ ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ

  • API์— ์ •์˜๋œ exception ์™ธ ํ•„์š”์— ๋”ฐ๋ผ ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ํด๋ž˜์Šค ์ž‘์„ฑ
  • ๋Œ€๋ถ€๋ถ„ Exception or RuntimeException ํด๋ž˜์Šค ์ƒ์†๋ฐ›์•„ ์ž‘์„ฑ
    • checked exception ํ™œ์šฉ
      • ๋ช…์‹œ์  ์˜ˆ์™ธ ์ฒ˜๋ฆฌ or throws ํ•„์š”
      • ์ฝ”๋“œ ๋ณต์žก → but, ์˜ค๋ฅ˜ ๋ฐœ์ƒ ๊ฐ€๋Šฅ์„ฑ ๋‚ฎ์Œ
    • runtime exception ํ™œ์šฉ
      • ๋ฌต์‹œ์  ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ
      • ์ฝ”๋“œ ๊ฐ„๊ฒฐ → but, ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ˆ„๋ฝ ๊ฐ€๋Šฅ์„ฑ ๋ฐœ์ƒ
  • ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ์žฅ์ 
    • ๊ฐ์ฒด ํ™œ์šฉ
      • ํ•„์š”ํ•œ ์ถ”๊ฐ€์ •๋ณด, ๊ธฐ๋Šฅ ํ™œ์šฉ ๊ฐ€๋Šฅ
    • ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ
      • ๋™์ผํ•œ ์ƒํ™ฉ์—์„œ ์˜ˆ์™ธ ๊ฐ์ฒด ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    • throws ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ด์šฉ
      • ์ค‘๊ฐ„ ํ˜ธ์ถœ ๋‹จ๊ณ„์—์„œ return ๋ถˆํ•„์š”
๋ฐ˜์‘ํ˜•

'STUDY > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Java] ํŒŒ์ผ ์ž…์ถœ๋ ฅ (File/IO)  (0) 2024.11.03
[Java] ์ปฌ๋ ‰์…˜ (Collection)  (1) 2024.10.30
[Java] ์ œ๋„ค๋ฆญ (Generic)  (2) 2024.10.28
[Java] ์ธํ„ฐํŽ˜์ด์Šค (Interface)  (0) 2024.10.26
[Java] ๋‹คํ˜•์„ฑ (Polymorphism)  (0) 2024.10.24
Comments