Spring/JAVA

Java ์˜ˆ์™ธ (Try-Catch / Try-Catch-Finally / Try with Resource Statements / Throws)

alsruds 2023. 9. 26. 03:02
๐Ÿฃ์ƒํ™œ์ฝ”๋”ฉ ๊ฐ•์˜๐Ÿฃ

Java ์˜ˆ์™ธ - https://www.youtube.com/playlist?list=PLuHgQVnccGMCrFJLxpjhE0N5tvOVxJuVB

 

๋ฌธ์ œ ์ƒํ™ฉ ํŒŒ์•…ํ•˜๊ธฐ

public class Main {
    public static void main(String[] args) {
        System.out.println(1);
        System.out.println(2/0); // ๋ฌธ์ œ์˜ ์ฝ”๋“œ
        System.out.println(3);
    }
}

์—๋Ÿฌ ๋ฐœ์ƒ ~

โžก๏ธ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค

 

Try - Catch ์ ์šฉํ•˜๊ธฐ

public class Main {
    public static void main(String[] args) {
        System.out.println(1);
        int[] scores = {10, 20, 30};

        try { // exception
            System.out.println(scores[3]);
            System.out.println(2 / 0);
        } catch(ArithmeticException e) { // 2/0 ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
            System.out.println("์ž˜๋ชป๋œ ๊ณ„์‚ฐ์ž…๋‹ˆ๋‹ค"+e.getMessage());
            e.printStackTrace();
        } catch(ArrayIndexOutOfBoundsException e) { // scores[3] ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
            System.out.println("๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ์Šต๋‹ˆ๋‹ค"+e.getMessage());
            e.printStackTrace();
        }
        System.out.println(3);
    }
}

โœ”๏ธ e.getMessage() : ์–ด๋–ค ์—๋Ÿฌ์ธ์ง€ ํ™•์ธํ•˜๊ธฐโœ”๏ธ e.printStackTrace() : ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅํ•˜๊ธฐ

โœ”๏ธ ๋ถ€๋ชจ Exception ์ด ์ž์‹ Exception (ex. ArithmeticException / ArrayIndexOutOfBoundsException) ์„ ํฌํ•จํ•˜์—ฌ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค → catch(Exception e) ๋กœ ๋Œ€์ฒด ๊ฐ€๋Šฅ

 

โ˜‘๏ธ ๋ณ€๊ฒฝ๋œ ์ฝ”๋“œ

public class Main {
    public static void main(String[] args) {
        ...
        try { // exception
            System.out.println(scores[3]);
            System.out.println(2 / 0);
        } catch(Exception e) { // ๋ณ€๊ฒฝ
            System.out.println("์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค");
        }
        ...
    }
}

 

Try - Catch - Finally ์ ์šฉํ•˜๊ธฐ

· Exception : Checked Exception & Unchecked Exception

· Checked Exception : ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์ˆ˜

· RunTimeException ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ Exception (ex. IOException) ์€ ๋ชจ๋‘ Checked Exception

· IOException : FileNotFoundException

 

โ˜‘๏ธ ํ”„๋กœ์ ํŠธ ๋‚ด ํŒŒ์ผ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ

import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        FileWriter f = null;
        try { // ํ•„์ˆ˜์ ์œผ๋กœ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํ•„์š”
             f = new FileWriter("data.txt");
            f.write("Hello Exception!");
            // close ํ•˜๊ธฐ ์ „ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— finally ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค
            // f.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // ํŒŒ์ผ ์กด์žฌ ์—ฌ๋ถ€ ํŒŒ์•…ํ•˜๊ธฐ
            if (f != null) {
                try {
                    f.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

ํŒŒ์ผ ์ƒ์„ฑ ์™„๋ฃŒ ~

 

Try with Resource Statements

โ˜‘๏ธ ์œ„์˜ Try - Catch - Finally ์™€ ๊ฐ™์€ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ

import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        try (FileWriter f = new FileWriter("data.txt") {
            f.write("Hello Exception!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

→ f.close() ๊ฐ€ ์‚ฌ๋ผ์กŒ๋‹ค

 

ThrowException

· ์˜ˆ์™ธ ์ƒ์„ฑํ•˜๊ธฐ : throws

· Java7 ๋ถ€ํ„ฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

โ˜‘๏ธ ์œ„์˜ Try - Catch - Finally / Try with Resource Statements ์™€ ๊ฐ™์€ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ

import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        FileWriter f = new FileWriter("./data.txt");
    }
}