Notice
Recent Posts
Recent Comments
Today
Total
01-27 02:15
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[CH08] ์˜ˆ์™ธ์ฒ˜๋ฆฌ(Exception handling) ๋ณธ๋ฌธ

๐Ÿ’ป Study !/JAVA

[CH08] ์˜ˆ์™ธ์ฒ˜๋ฆฌ(Exception handling)

WonderJay 2022. 8. 2. 17:32
728x90
๋ฐ˜์‘ํ˜•
SMALL

๋ณธ ํฌ์ŠคํŒ…์€ ์ž๋ฐ”์˜ ์ •์„ ๊ต์žฌ๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉฐ, ๊ฐ„๋‹จํžˆ ์ •๋ฆฌ/๊ธฐ๋ก ์šฉ๋„๋กœ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ˜น์—ฌ, ์ž˜๋ชป๋œ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ์ง€์ ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 


1. ์˜ˆ์™ธ์ฒ˜๋ฆฌ(exception handling)

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

 

  ์ž๋ฐ”์—์„œ๋Š” ์‹คํ–‰ ์‹œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์˜ค๋ฅ˜๋ฅผ ํด๋ž˜์Šค๋กœ ์ •์˜ํ•˜์˜€๋‹ค. Object ํด๋ž˜์Šค์˜ ์ž์†์ธ Exception ๊ณผ Error ํด๋ž˜์Šค์™€ ๊ด€๋ จํ•œ ๊ณ„์ธต ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

https://finewoo.tistory.com/22

  RuntimeException ํด๋ž˜์Šค๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ์ฝ”๋“œ ์ƒ ์‹ค์ˆ˜๋กœ๋ถ€ํ„ฐ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์™ธ๋“ค์œผ๋กœ ArrayIndexOutOfBoundsException, NullPointerException, ClassCastException, ArithmeticException ์˜ ๊ฒฝ์šฐ์— ๋ฐœ์ƒํ•œ๋‹ค. 

  Exception ํด๋ž˜์Šค๋Š” ์™ธ๋ถ€ ์˜ํ–ฅ์œผ๋กœ๋ถ€ํ„ฐ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์œผ๋กœ User ์˜ ๋™์ž‘์— ์˜ํ•ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. FileNotFoundException, ClassNotFoundException, DataFormatException ๋“ฑ์— ํ•ด๋‹นํ•œ๋‹ค.

 

  ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด try - catch ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์™ธ ์ฒ˜๋ฆฌ(exception handling)๋ผ๋Š” ๊ฒƒ์€ ์˜ˆ์™ธ ๋ฐœ์ƒ์œผ๋กœ๋ถ€ํ„ฐ์˜ ๋น„์ •์ƒ์ ์ธ ๋™์ž‘์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ์™ธ๊ฐ€ ์ž˜ ์ฒ˜๋ฆฌ๋˜์ง€ ๋ชปํ•ด์„œ ๋น„์ •์ƒ์ ์ธ ๋™์ž‘์ด ๋ฐœ์ƒํ•˜๋ฉด JVM ์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ธฐ(UncaughtExceptionHandler)๊ฐ€ ๋ฐ›์•„์„œ ์˜ˆ์™ธ ์›์ธ์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•ด์ค€๋‹ค. 

class source{
    public static void main(String[] args) {
        int num = 100;
        int result = 0;
        for(int i = 0 ; i < 10; i ++){
            try{
                result = num/(int)(Math.random()*10);
                System.out.println(result);
            } catch (ArithmeticException e){
                System.out.println("0");
            }
        }
    }
}

 ์œ„์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์˜ˆ์ œ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” zero - dividing exception ์— ๋Œ€ํ•ด์„œ ์ฒ˜๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค.  try  - catch ๊ตฌ๋ฌธ์ด ์‹คํ–‰๋  ๋•Œ์˜ ํ๋ฆ„์„ ์‚ดํŽด๋ณด์ž. try ๋ธ”๋Ÿญ ๋‚ด์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋จผ์ € ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ์™€ ์ผ์น˜ํ•˜๋Š” catch ๋ธ”๋Ÿญ์„ ํƒ์ƒ‰ํ•˜๊ณ , ๋งŒ์•ฝ ํ•ด๋‹นํ•˜๋Š” ๋ธ”๋Ÿญ์ด ์žˆ๋‹ค๋ฉด ์ˆ˜ํ–‰ํ•œ ๋‹ค์Œ try - catch ๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ€์„œ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๋งŒ์•ฝ ์ฐพ์ง€ ๋ชปํ•˜๋ฉด ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ์•ˆ๋˜๋Š” ๊ฒƒ์ด๋‹ค. try ๋ธ”๋Ÿญ ๋‚ด์—์„œ ์•„์˜ˆ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋ƒฅ ๋ฐ”๊นฅ์œผ๋กœ ๋น ์ ธ๋‚˜๊ฐ€๋Š” ๊ฒƒ์ด๋‹ค.

 catch ๋ธ”๋Ÿญ์„ ๋งŒ๋‚ฌ์„ ๋•Œ, ์ฐจ๋ก€๋Œ€๋กœ catch ๋ธ”๋Ÿญ์˜ ( ) ๋‚ด์˜ ์ฐธ์กฐ๋ณ€์ˆ˜์˜ ์ข…๋ฅ˜์™€ ์ƒ์„ฑ๋œ ์˜ˆ์™ธ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•˜์—ฌ instanceof ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ true ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ธ”๋Ÿญ์„ ๋งŒ๋‚  ๋•Œ ๊นŒ์ง€ ์ฐจ์ธฐ์ฐจ์ธฐ ๊ฒ€์‚ฌํ•œ๋‹ค.  ๋ชจ๋“  ์˜ˆ์™ธ ํด๋ž˜์Šค๋Š” Exception ํด๋ž˜์Šค์˜ ์ž์†์ด๊ธฐ ๋–„๋ฌธ์— catch ๋ธ”๋Ÿญ์˜ ( ) ๋‚ด์— Exception ํด๋ž˜์Šค ํƒ€์ž…์˜ ์ฐธ์กฐ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋ฉด ์–ด๋–ค ์ข…๋ฅ˜์˜ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ catch ๋ธ”๋Ÿญ์— ์˜ํ•ด์„œ ์ฒ˜๋ฆฌ๊ฐ€ ๋œ๋‹ค.

class source{
    public static void main(String[] args) {
        int num = 100;
        int result = 0;
        for(int i = 0 ; i < 10; i ++){
            try{
                result = num/(int)(Math.random()*10);
                result = 0/0;
                System.out.println(result);
            } catch (Exception e){
                System.out.println("0");
            }
        }
    }
}

 

  ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ์ƒ์„ฑ๋˜๋Š” ์˜ˆ์™ธ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์—๋Š” ์˜ˆ์™ธ ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์žˆ๋‹ค. ์ด๋Š” printStackTrace() : ์˜ˆ์™ธ๋ฐœ์ƒ ์‹œ ํ˜ธ์ถœ ์Šคํƒ(Call Stack) ๋‚ด์— ์žˆ๋˜ ๋ฉ”์„œ๋“œ ์ •๋ณด ๋ฐ ์˜ˆ์™ธ ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์™€ getMessage() : ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์— ์ €์žฅ๋œ ๋ฉ”์„ธ์ง€๋ฅผ ์–ป๋Š”๋‹ค. ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค. 

class source{
    public static void main(String[] args) {
        int num = 100;
        int result = 0;
        for(int i = 0 ; i < 10; i ++){
            try{
                result = num/(int)(Math.random()*10);
                result = 0/0;
                System.out.println(result);
            } catch (Exception e){
                e.printStackTrace();
                System.out.println("error : " + e.getMessage());
            }
        }
    }
}

 

  JDK1.7 ๋ถ€ํ„ฐ | ๊ธฐํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•˜๋‚˜์˜ catch ๋ธ”๋Ÿญ์œผ๋กœ ์—ฌ๋Ÿฌ catch ๋ธ”๋Ÿญ์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ, ์ด๋ฅผ multi - catch ๋ธ”๋Ÿญ ์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋‹ค๋งŒ ์—ฐ๊ฒฐ๋œ ์˜ˆ์™ธ ํด๋ž˜์Šค๊ฐ€ ์„œ๋กœ ์กฐ์ƒ - ์ž์† ๊ด€๊ณ„๋ผ๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ค‘์š”ํ•œ ๊ฒƒ์€ ํ•˜๋‚˜์˜ catch ๋ธ”๋Ÿญ์œผ๋กœ ์—ฌ๋Ÿฌ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋–„๋ฌธ์—, ์‹ค์ œ ์–ด๋–ค ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์ฒ˜๋ฆฌ๋œ ๊ฒƒ์ธ์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ž˜์„œ instanceof ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ ์–ด๋–ค ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ธ์ง€ ์ฒดํฌํ•œ ๋‹ค์Œ ๊ฐœ๋ณ„ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€๋ฐ, ์‚ฌ์‹ค ์ด๋ ‡๊ฒŒ๊นŒ์ง€ ํ•ด์„œ catch ๋ธ”๋Ÿญ์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น  ์ด์œ ๋Š” ํฌ๊ฒŒ ์—†๋‹ค. 

 

  throw ๋ฅผ ์ด์šฉํ•˜์—ฌ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ์™ธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“  ๋‹ค์Œ, throw ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐœ์ƒ์‹œํ‚ค๋ฉด ๋œ๋‹ค.

class source{
    public static void main(String[] args) {
        try{
            Exception e = new Exception("hi~~");
            throw e;
        } catch (Exception e){
            System.out.println("error : " + e.getMessage());
            e.printStackTrace();
        }
    }
}

 

  ๋ฉ”์„œ๋“œ์— ์˜ˆ์™ธ ์„ ์–ธํ•˜๊ธฐ

try - catch ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์˜ˆ์™ธ๋ฅผ ๋ฉ”์„œ๋“œ์— ์„ ์–ธํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ๋ฉ”์„œ๋“œ์˜ ์„ ์–ธ๋ถ€์— throws ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐœ์ƒ ๊ฐ€๋Šฅํ•œ ์˜ˆ์™ธ ์‚ฌํ•ญ์„ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค. ์˜ˆ์™ธ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ด๋ฉด ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.

  ์ด์™€ ๊ฐ™์ด ์ž๋ฐ”์—์„œ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ, ๋ฐœ์ƒ ๊ฐ€๋Šฅํ•œ ์˜ˆ์™ธ๋ฅผ ๋ฉ”์„œ๋“œ ์„ ์–ธ๋ถ€์— ๋ช…์‹œํ•˜๋„๋ก ํ•˜์—ฌ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ด์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ๊ทธ๋žจ์„ ๋ณด๋‹ค ๊ฒฌ๊ณ ํ•˜๊ฒŒ ์„ค๊ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค.

import java.io.*;

class source{
    public static void main(String[] args) {
        File f = createFile(args[0]);
        System.out.println(f.getName() + " is generated.");
    }
    static File createFile(String fileName){
        try {
            if (fileName==null || fileName.equals("") || fileName.equals("no"))
                    throw new Exception("Filename is invalid.");
        } catch(Exception e){
            fileName = "noname.txt";
        } finally {
            File f = new File(fileName);
            createNewFile(f);
            return f;
        }
    }
    static void createNewFile(File f){
        try{
            f.createNewFile();
        } catch(Exception e){  }
    }
}

์œ„์˜ ์ฝ”๋“œ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ ๋ฉ”์„œ๋“œ์—์„œ ์ง์ ‘ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค. createFile ๋ฉ”์„œ๋“œ์—์„œ๋Š” ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ ์ง€ ์•„๋‹Œ์ง€๋ฅผ ์•Œ์ˆ˜ ์—†๋Š”๋ฐ, ์ ์ ˆ์น˜ ๋ชปํ•œ ํŒŒ์ผ๋„ค์ž„์ด argument ๋กœ ๋“ค์–ด์˜ค๋ฉด catch ๋ธ”๋Ÿญ์—์„œ noname.txt ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค. finally ๋ธ”๋Ÿญ์—์„œ๋Š” ์˜ˆ์™ธ ๋ฐœ์ƒ๊ณผ ๊ด€๋ จ์—†์ด ํŒŒ์ผ์„ ๋ฌด์กฐ๊ฑด ์ƒ์„ฑํ•œ๋‹ค.

 

intelliJ ์—์„œ ๋ช…๋ น์ค„ ์ธ์ˆ˜ ์„ค์ •ํ•˜๊ธฐ

๋”๋ณด๊ธฐ

ALT + Shift + F10 ์„ ๋ˆ„๋ฅธ ๋‹ค์Œ, ์ž‘์—…์ค‘์ธ ํ”„๋กœ์ ํŠธ ๋ช…์˜ ํ™”์‚ดํ‘œ๋ฅผ ๋ˆ„๋ฅด๊ณ  Edit ํด๋ฆญ ์ดํ›„ Program argument ๋ž€์— ๊ธฐ์ž…ํ•˜๋ฉด ๋œ๋‹ค.

 

 

08.1.8 finally ๋ธ”๋Ÿญ

  ์˜ˆ์™ธ์™€ ์ƒ๊ด€์—†์ด ์‹คํ–‰๋˜์–ด์•ผ ํ•  ์ฝ”๋“œ๋ฅผ ํฌํ•จ์‹œํ‚ฌ ๋ชฉ์ ์œผ๋กœ finally ๋ธ”๋Ÿญ์„ ์‚ฌ์šฉํ•œ๋‹ค. ๋ณดํ†ต try - catch - finally ์ˆœ์„œ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด try - catch - finally ์ˆœ์„œ๋กœ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ฒ ์ง€๋งŒ, ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” try - finally ์ˆœ์„œ๋กœ ์‹คํ–‰๋œ๋‹ค.

class source {
    public static void main(String[] args) {
        try {
            startInstall();
            copyFiles();
            deleteTempFiles();
        } catch (Exception e) {
            e.printStackTrace();
            deleteTempFiles();
        }
    }

    static void startInstall() {

    }

    static void copyFiles() {

    }

    static void deleteTempFiles() {
        
    }
}

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๋„ ๋ฐ˜๋“œ์‹œ ์ž„์‹œ ํŒŒ์ผ๋“ค์ด ์‚ญ์ œ๋  ์ˆ˜ ์žˆ๋„๋ก catch ๋ธ”๋Ÿญ์— deleteTempFiles() ๋ฉ”์„œ๋“œ๊ฐ€ ๋“ค์–ด๊ฐ”๋‹ค. ์ด์™€ ๊ฐ™์ด ์˜ˆ์™ธ ๋ฐœ์ƒ์— ๊ด€๊ณ„์—†์ด deleteTempFiles() ๋ฉ”์„œ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์— finally ๋ธ”๋Ÿญ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ณด๋‹ค ์ข‹๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋„๋ก ํ•˜์ž.

class source {
    public static void main(String[] args) {
        try {
            startInstall();
            copyFiles();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            deleteTempFiles();
        }
    }

    static void startInstall() {

    }

    static void copyFiles() {

    }

    static void deleteTempFiles() {

    }
}

์œ ์˜ํ•ด์•ผํ•  ์ ์€ try ๋ธ”๋Ÿญ์˜ ์‹คํ–‰ ๋„์ค‘์— return ๋ฌธ์ด ์‹คํ–‰๋˜๋”๋ผ๋„ finally ๋ธ”๋Ÿญ์˜ ๋ฌธ์žฅ์ด ์‹คํ–‰๋œ ๋‹ค์Œ์— ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ๋ฉ”์„œ๋“œ๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. catch ๋ธ”๋Ÿญ ๋ฌธ์žฅ ์ˆ˜ํ–‰ ๋„์ค‘์— return ๋ฌธ์„ ๋งŒ๋‚˜๋”๋ผ๋„ ๋™์ผํ•˜๊ฒŒ finally ๋ธ”๋Ÿญ์˜ ๋ฌธ์žฅ๋“ค์ด ์ˆ˜ํ–‰๋œ๋‹ค. 

 

08.1.9 ์ž๋™ ์ž์› ๋ฐ˜ํ™˜ ( try - with - resources ๋ฌธ )

  JDK1.7 ๋ถ€ํ„ฐ try - with - resources ๋ฌธ์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค. ์ด๋Š” I/O ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์œ ์šฉํ•˜์ง€๋งŒ, ์ง€๊ธˆ์€ ๊ทธ๋ƒฅ ์•Œ์•„๋‘๊ธฐ๋งŒ ํ•˜์ž... ์ž…์ถœ๋ ฅ ํด๋ž˜์Šค ์ค‘์—๋Š” ์ž์›์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ ์ดํ›„์— ๋‹ซ์•„์ค˜์•ผํ•˜๋Š” ๊ฒƒ๋“ค์ด ์žˆ๋‹ค. 

try{
    fis = new FileInputStream("score.dat");
    dis = new DataInputStream(fis);
} catch (IOException ie){
    ie.printStackTrace();
} finally {
    dis.close();
}

DataInputStream ์„ ์ด์šฉํ•ด์„œ ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ๋•Œ, ์œ„์™€ ๊ฐ™์ด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๋„ InputStream ์ด ๋‹ซํžˆ๋„๋ก finally ์•ˆ์— close ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ค‘์š”ํ•œ ๊ฒƒ์€ close() ์ž์ฒด์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์œ„ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค.

try{
    fis = new FileInputStream("score.dat");
    dis = new DataInputStream(fis);
} catch (IOException ie){
    ie.printStackTrace();
} finally {
    try{
        if(dis!=null)
            dis.close();
    } catch(IOException ie){
        ie.printStackTrace();
    }
}

๋‹ค๋งŒ ์•„๋ฌด๋ž˜๋„ finally ๋ธ”๋Ÿญ ์•ˆ์— try - catch ๋ฌธ์„ ์ถ”๊ฐ€ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ๋‚ฎ์•„์ง„ ๊ฒƒ์€ ์‚ฌ์‹ค์ด๋‹ค.  ์ด๋ฅผ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด ์ž๋™์œผ๋กœ ์ž์›์„ ๋ฐ˜ํ™˜ํ•˜๋Š” try - with - resources ๋ฌธ์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค. try ๋ธ”๋Ÿญ์˜ ๊ด„ํ˜ธ ์•ˆ์— ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฌธ์žฅ์„ ๋„ฃ์œผ๋ฉด try ๋ธ”๋Ÿญ์„ ๋ฒ—์–ด๋‚˜๋Š” ์ˆœ๊ฐ„ ์ž๋™์œผ๋กœ close() ๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ๋ฆฌ์†Œ์Šค๊ฐ€ ๋ฐ˜๋‚ฉ๋˜๋„๋ก ๊ตฌํ˜„๋œ๋‹ค. ์ด์™€ ๊ฐ™์ด try - with - resources ๋ฌธ์— ์˜ํ•˜์—ฌ ๊ฐ์ฒด์˜ close() ๊ฐ€ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋ ค๋ฉด ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ AutoCloseable ์ด๋ผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด์—ฌ์•ผ๋งŒ ํ•œ๋‹ค.  ํ•˜์ง€๋งŒ close() ๋˜ํ•œ Exception ์„ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์ด ์ค‘์š”ํ•˜๋‹ค. 

 

class source{
    public static void main(String[] args) {
        try(CloseableResource cr = new CloseableResource()) {
            cr.exceptionWork(false);
        } catch(WorkException e){
            e.printStackTrace();
        } catch(CloseException e){
            e.printStackTrace();
        }
        System.out.println();

        try(CloseableResource cr = new CloseableResource()){
            cr.exceptionWork(true); // ์˜ˆ์™ธ ๋ฐœ์ƒ
        } catch(WorkException e){
            e.printStackTrace();
        } catch (CloseException e){
            e.printStackTrace();
        }
    }
}

class CloseableResource implements AutoCloseable{
    public void exceptionWork(boolean exception) throws WorkException{
        System.out.println("call exceptionWork(" + exception + ")");

        if(exception)
            throw new WorkException("WorkException occurs!!");
    }
    public void close() throws CloseException{
        System.out.println("call close()");
        throw new CloseException("CloseException occurs!!");
    }
}

class WorkException extends Exception{
    WorkException(String msg) { super(msg);}
}
class CloseException extends Exception{
    CloseException(String msg) {super(msg);}
}

  ์ฒซ ๋ฒˆ์งธ try - catch ๋ฌธ์—์„œ๋Š” close() ์—์„œ๋งŒ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œ์ผฐ๋‹ค. ์ด์— ๋”ฐ๋ฅธ ๋ฉ”์„ธ์ง€๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

CloseException: CloseException occurs!!
at CloseableResource.close(source.java:31)
at source.main(source.java:5)

 

  ๋‘ ๋ฒˆ์งธ try - catch ๋ฌธ์—์„œ๋Š” close() ์™€ exceptionWork() ์—์„œ ๋ชจ๋‘ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œ์ผฐ๋‹ค. ์ด์— ๋”ฐ๋ฅธ ๋ฉ”์„ธ์ง€๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

WorkException: WorkException occurs!!
at CloseableResource.exceptionWork(source.java:27)
at source.main(source.java:13)
Suppressed: CloseException: CloseException occurs!!
at CloseableResource.close(source.java:31)
at source.main(source.java:12)

 

close() ์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋Š” Suppressed ๋ผ๋Š” ๋จธ๋ฆฌ๋ง๊ณผ ์ถœ๋ ฅ์ด ๋˜์—ˆ๋Š”๋ฐ, ๋‘ ์˜ˆ์™ธ๋Š” ๋™์‹œ์— ๋ฐœ์ƒํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์‹ค์ œ๋กœ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋Š” WorkException ์œผ๋กœ ํ•˜๊ณ , CloseException ์€ suppresed ์˜ˆ์™ธ๋กœ ๋‹ค๋ฃฌ๋‹ค. suppressed ์˜ˆ์™ธ์— ๋Œ€ํ•œ ์ •๋ณด๋Š” WorkException ์— ์ €์žฅ๋œ๋‹ค. try - catch ๋ฌธ์„ ์‚ฌ์šฉํ–ˆ์—ˆ๋”๋ผ๋ฉด WorkException ์€ ๋ฌด์‹œ๋˜๊ณ  CloseException ์— ๋Œ€ํ•œ ๋‚ด์šฉ๋งŒ ์ถœ๋ ฅ๋˜์—ˆ์„ ๊ฒƒ์ด๋‹ค. ( ๋‚˜์ค‘์— ๋‹ค์‹œ ๋Œ์•„์™€์„œ ์ƒ๊ฐํ•ด๋ณด์ž... )

 

[1.10 ์‚ฌ์šฉ์ž์ •์˜ ์˜ˆ์™ธ ๋งŒ๋“ค๊ธฐ]

  ๊ธฐ์กด์— ์ •์˜๋œ ์˜ˆ์™ธ ํด๋ž˜์Šค๊ฐ€ ์•„๋‹Œ ์ƒˆ๋กœ์šด ์˜ˆ์™ธ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ๋ณดํ†ต Exception ํด๋ž˜์Šค๋‚˜ RuntimeException ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ๋งŒ๋“ ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ Exception ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ checked exception ์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•˜์ง€๋งŒ ์š”์ฆ˜์—๋Š” RuntimeException ์„ ์ƒ์†๋ฐ›์•„ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์„ ํƒ์ ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ ๋‹ค๊ณ  ํ•œ๋‹ค. checked exception ์€ ๋ฐ˜๋“œ์‹œ ๋ณ„๋„์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋งŒ์•ฝ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”์น˜ ์•Š์€ ๊ฒฝ์šฐ์—๋„ try-catch ๋ฅผ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•˜๋ฏ€๋กœ ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•ด์ง„๋‹ค. ์ดˆ๊ธฐ์— checked exception ์œผ๋กœ ์ฃผ๋กœ ๋งŒ๋“ค์—ˆ์ง€๋งŒ ๊ทผ๋ž˜์— ์ข…์ข… unchecked exception ์œผ๋กœ ๋งŒ๋“œ๋Š” ์ด์œ ๋Š” ์ฃผ๋กœ ๋ชจ๋ฐ”์ผ์ด๋‚˜ ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์ž๋ฐ”๊ฐ€ ์“ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š”์— ๋”ฐ๋ผ์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์—ฌ๋ถ€๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.

class source{
    public static void main(String[] args) {
        try{
            startInstall();
            copyFile();
        } catch (SpaceException e) {
            System.out.println("error msg : " + e.getMessage());
            e.printStackTrace();
            System.out.println("you have to obtain more install spaces.");
        } catch(MemoryException me){
            System.out.println("error msg : " + me.getMessage());
            me.printStackTrace();
            System.gc(); // Garbage Collection ์„ ์ˆ˜ํ–‰ํ•ด์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋Š˜๋ ค์ฃผ๋Š” ๊ฒƒ.
            System.out.println("try again!");
        } finally {
            deleteTempFiles();
        }
    }
    static void startInstall() throws SpaceException, MemoryException{
        if(!enoughSpace())
            throw new SpaceException("not enough install spaces.");
        if(!enoughMemory())
            throw new MemoryException("not enough memory spaces.");
    }
    static void copyFile() {}
    static void deleteTempFiles(){}
    static boolean enoughSpace(){
        //
        return false;
    }
    static boolean enoughMemory(){
        //
        return true;
    }
}

class SpaceException extends Exception {
    SpaceException(String msg){
        super(msg);
    }
}
class MemoryException extends Exception{
    MemoryException(String msg){
        super(msg);
    }
}

 

[1.11 ์˜ˆ์™ธ ๋˜๋˜์ง€๊ธฐ; exception re-throwing]

  ํ•œ ๋ฉ”์„œ๋“œ์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์™ธ๊ฐ€ ์—ฌ๋Ÿฟ์ธ ๊ฒฝ์šฐ์—, ์ผ๋ถ€๋Š” try-catch ๋ฌธ์„ ์ด์šฉํ•ด์„œ ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ์„ ์–ธ๋ถ€์—์„œ ์ง€์ •ํ•ด์„œ ํ˜ธ์ถœํ•œ ๋ฉ”์„œ๋“œ์—์„œ ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ•œ๋‹ค. ์ด๋Š” ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•œ ๋‹ค์Œ ์ธ์œ„์ ์œผ๋กœ ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ ๋ฐœ์ƒ์‹œํ‚ค๋Š” exception re-throwing ์„ ์ด์šฉํ•ด์„œ ๊ฐ€๋Šฅํ•˜๋‹ค. ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ ๋ฉ”์„œ๋“œ์™€ ํ˜ธ์ถœํ•œ ๋ฉ”์„œ๋“œ ์–‘์ชฝ์—์„œ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•ด์•ผํ•  ๋•Œ ์˜ˆ์™ธ ๋˜๋˜์ง€๊ธฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋‹ค๋งŒ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ๋ฉ”์„œ๋“œ์—์„œ๋Š” try - catch ๋ฌธ์„ ์ด์šฉํ•ด์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒƒ๊ณผ ๋™์‹œ์— ๋ฉ”์„œ๋“œ ์„ ์–ธ๋ถ€์— ๋ฐœ์ƒํ•  ์˜ˆ์™ธ๋ฅผ throws ์— ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

class source{
    public static void main(String[] args) {
        try{
            method1();
        } catch(Exception e){
            System.out.println("Exception is handled at main method.");
        }
    }

    static void method1() throws Exception {
        try{
            throw new Exception();
        } catch (Exception e) {
            System.out.println("Exception is handled at method1.");
            throw e; // re - throwing
        }
    }
}

๋ฐ˜ํ™˜๊ฐ’์ด ์žˆ๋Š” return ๋ฌธ์˜ ๊ฒฝ์šฐ์—๋Š” catch ๋ธ”๋Ÿญ์—๋„ return ๋ฌธ์ด ์žˆ์–ด์•ผ ํ•˜์ง€๋งŒ, catch ๋ธ”๋Ÿญ์—์„œ exception re-throwing ์„ ํ•˜์—ฌ ํ˜ธ์ถœํ•œ ๋ฉ”์„œ๋“œ๋กœ exception ์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” return ๋ฌธ์ด ์—†์–ด๋„ ๋œ๋‹ค.


References

http://www.yes24.com/Product/Goods/24259565

 

Java์˜ ์ •์„ - YES24

์ตœ๊ทผ 7๋…„๋™์•ˆ ์ž๋ฐ” ๋ถ„์•ผ์˜ ๋ฒ ์ŠคํŠธ ์…€๋Ÿฌ 1์œ„๋ฅผ ์ง€์ผœ์˜จ `์ž๋ฐ”์˜ ์ •์„`์˜ ์ตœ์‹ ํŒ. ์ €์ž๊ฐ€ ์นดํŽ˜์—์„œ 12๋…„๊ฐ„ ์ง์ ‘ ๋…์ž๋“ค์—๊ฒŒ ๋‹ต๋ณ€์„ ํ•ด์˜ค๋ฉด์„œ ์ดˆ๋ณด์ž๊ฐ€ ์–ด๋ ค์›Œํ•˜๋Š” ๋ถ€๋ถ„์„ ์ž˜ ํŒŒ์•…ํ•˜๊ณ  ์“ด ์ฑ…. ๋ฟ๋งŒ ์•„

www.yes24.com

 

 

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments