Post

Try-catch, Exception

Error(์˜ค๋ฅ˜) ๐Ÿ†š Exception(์˜ˆ์™ธ)

Screenshot 2024-07-31 at 23 01 57

๋‘˜ ๋‹ค Throwable์˜ class

๐Ÿ’ฅ Error(์˜ค๋ฅ˜)

  • ์ปดํ“จํ„ฐ ์ž์ฒด์˜ ๋ฌธ์ œ(cpu, memory ๋“ฑ)
  • JVM์—์„œ ๊ธฐ์ธํ•œ ๋ฌธ์ œ โžก๏ธ ์ฝ”๋“œ์˜ ๋ฌธ์ œ๊ฐ€ ์•„๋‹˜
  • ํ†ต์ œ ๋ถˆ๊ฐ€๋Šฅ โŒ
  • ํด๋ž˜์Šค๋กœ ๊ด€๋ฆฌ๋˜๊ณ  ์žˆ๋‹ค.
  • example: OutOfMemoryError

๐Ÿ’ฅ Exception(์˜ˆ์™ธ)

  • ํ†ต์ œ ๊ฐ€๋Šฅ โญ•๏ธ ๐ŸŸฐ ์˜ˆ์™ธ์ฒ˜๋ฆฌ
  • ํด๋ž˜์Šค๋กœ ๊ด€๋ฆฌ๋˜๊ณ  ์žˆ๋‹ค.

    • Check Exception: ๊ผญ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ์˜ˆ์™ธ, will not be coompiled
      • IOException, SQL Exception
      • ArrayBoundOutException ๋ฐฐ์—ด 10๊ฐœ์ธ๋ฐ 11๋ฒˆ์งธ ์ธ๋ฑ์Šค ์ถ”์ถœ
    • Uncheck Exception: ๋ฐ˜๋“œ์‹œ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š” ์˜ˆ์™ธ๋Š” ์•„๋‹˜
      • NullPointException
      • RunTimeException
      • NullPointerException
      • IndexOutOfBoundException
  • ํ˜„์‹ค์„ธ๊ณ„์— ๋งž์ง€ ์•Š์€ ์š”์ฒญ(์‚ฌ๋žŒ ํ‚ค ์Œ์ˆ˜๊ฐ’ ๋„ฃ๊ธฐ, 3๋ช…์˜ ํ‹ฐ์ผ“๊ฐ’ ์ค‘ 2๋ช…๋งŒ ๊ฒฐ์ œ)
  • ์™ธ๋ถ€ ํ™˜๊ฒฝ ๋ณ€ํ™”(์™ธ๋ถ€ ์ธํ”„๋ผ ์‚ฌ๊ณ  ๋ฐœ์ƒ, ์ฝ๊ณ  ์žˆ๋Š” ํŒŒ์ผ์ด ์‚ญ์ œ๋œ๋‹ค๋˜๊ฐ€โ€ฆ)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class ExceptionTest {
    public static void main(String[] args) throws FileNotFoundException {
        System.out.println("Main method run start");
        //๐Ÿ’ฅ unCheckException
        //ArrayBoundOutException
        makeUncheckedException();
        //๐Ÿ’ฅ checkedException
        //๋นจ๊ฐ„์ค„ ์ œ๊ฑฐ ์œ„ํ—ค throws FileNotFoundException
        makeCheckedException();
        System.out.println("Main method end");
    }
    //๐Ÿ’ฅ unCheckException
    public static void makeUncheckedException(){
        int[] intArr= {1,2,3,4,5,6,7,8,9,10};
        int index=11;

        System.out.println(intArr[index]);
    }
    //๐Ÿ’ฅ checkedException
    public static void makeCheckedException() throws FileNotFoundException {
        File file= new File("src/chap50_errorexception/checkedExcpetion.txt");
        FileInputStream fs= new FileInputStream(file);
        //๋ฐ”๋กœ FileInputStream์— ๋นจ๊ฐ„์ค„
        //๋นจ๊ฐ„์ค„ ์—†์• ๊ธฐ ์œ„ํ•ด์„œ๋Š” throws FileNotFoundException
        //CheckedException์„ ํ•˜๋„๋ก ์š”์ฒญ๋ฐ›์•„ try-catch ํ•˜๊ฑฐ๋‚˜ throw๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค.
    }

}

๐Ÿ’ฅ Error(์˜ค๋ฅ˜)์ด๋‚˜ Exception(์˜ˆ์™ธ) throw

๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— newํ•„์š”
ํ•จ์ˆ˜ ๋‚˜๊ฐˆ ๋•Œ return โŒ throw โญ•๏ธ

โœ… Try-catch

try: ๋กœ์ง ์‹คํ–‰ catch: ์˜ˆ์™ธ ๋ฐœ์ƒํ•˜๋ฉด ์‹คํ–‰

try-catch ์‚ฌ์šฉ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class TryCatch1 {
    public static void main(String[] args) {
        System.out.println("Main method run start");
        try {
            int i = 0; //๋งŒ์•ฝ i๊ฐ€ 50์˜ ์•ฝ์ˆ˜์˜€๋‹ค๋ฉด try๋ฌธ์ด ์‹คํ–‰๋˜์—ˆ์„ ๊ฒƒ์ด๋‹ค.
            int data = 50 / i; //๐Ÿ’ฅ exception ๋ฐœ์ƒ
            System.out.println("data"+ data);
        } catch(ArithmeticException e){ // ๐Ÿ’Š error๊ฐ€ ArithmeticException์ด๋ผ๋ฉด catch ์‹คํ—น
            System.out.println("Cannot divide by 0");
            System.out.println("data"+ 0);
            e.printStackTrace(); //์–ด๋–ค error๋ฐœ์ƒํ–ˆ๋Š”์ง€ ๋‚ด์šฉ ๋ณด๊ธฐ
        }

        System.out.println("Main method end");
    }
}

โœ… Try-catch ๋‹ค์ค‘์ฒ˜๋ฆฌ

catch๋ฌธ์ด ์—ฌ๋Ÿฌ๊ฐœ

try-catch ๋Š” ์—ฌ๋Ÿฌ๊ฐœ ์„ ์–ธ๋„ ๊ฐ€๋Šฅ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class TrCatch2 {
    public static void main(String[] args) {
        System.out.println("Main method running start");
        List<String> stringList= new ArrayList<>();

        stringList.add("Hello ");
        stringList.add("World ");
        stringList.add("I ");
        stringList.add("Love ");
        stringList.add(null); //๐Ÿ’ฅException ๋ฐœ์ƒ, null์€ ๋Œ€๋ฌธ์ž๋กœ ๋งŒ๋“ค์ˆ˜๊ฐ€ ์—†์ž–์•„. ์ด ์ดํ›„๋กœ stringList.add("Coding");๋Š” ์‹คํ–‰์ด ์•ˆ ๋จ.
        stringList.add("Coding");

        for(int i=0; i<stringList.size()+5; i++){ //๐Ÿ’ฅException ๋ฐœ์ƒ, arraysize๋ณด๋‹ค ๋” ๋งŽ์ด ์ถœ๋ ฅํ•˜๋ ค๊ณ  ํ•จ
            try{
                String str= stringList.get(i);
                System.out.println(str.toUpperCase());
            } catch(NullPointerException e){ //๐Ÿ’Š
                System.out.println("Cannot change null to uppercase.");
            } catch(IndexOutOfBoundsException e){ //๐Ÿ’Š
                System.out.println("Index cannot be over arraysize.");
                break;
            }
        }
        System.out.println("Main method end");
    }
}


โ˜‘๏ธ ์ฒซ ๋ฒˆ์งธ exception ๋ฐœ์ƒ ์ดํ›„๋กœ๋Š” ์ง„ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.

1
2
3
4
5
6
7
8
9
            //๐Ÿ’ฅ ๋‚˜๋ˆ„๊ธฐ Arithmatic exception
            //โญ๏ธ ์ฒซ ๋ฒˆ์งธ exception ๋ฐœ์ƒ ์ดํ›„๋กœ๋Š” ์ง„ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.
            int i=0;
            int data= 50/i;

            //โญ์–˜๋Š” ์‹คํ–‰ ์•ˆ ๋จ
            //๐Ÿ’ฅString null
            String str= null;
            System.out.println(str.toUpperCase());

โ˜‘๏ธ try catch ๋‹ค์ค‘ OR๋กœ ์„ ์–ธ |

๋‘ ์˜ค๋ฅ˜ ์ค‘ ํ•˜๋‚˜ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๋‚˜์„œ catch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class TryMultiCatch {
    public static void main(String[] args) {
        try{
//
        //์ด๋ ‡๊ฒŒ ํ•˜๋‚˜ํ•˜๋‚˜ Exception์ •์˜ํ•ด๋„ ๋˜์ง€๋งŒ
//        }catch(FileNotFoundException e){
//            System.out.println("FileNotFoundException");
//            e.printStackTrace();
//        }catch(ArithmeticException e){
//            System.out.println("ArithmeticException");
//        }catch(NullPointerException e){
//            System.out.println("NullPointerException");
//        }
        }catch(FileNotFoundException e){ //๐Ÿ’Š
            System.out.println("FileNotFoundException");
            e.printStackTrace();
            //OR ์‚ฌ์šฉํ•ด์„œ Exception
        }catch(ArithmeticException | NullPointerException e ){ //๐Ÿ’Š
            System.out.println("ArithmeticException or NullPointerException");
        }
    }
}

โ˜‘๏ธ ํŒŒ์ผ์„ ์ฝ์–ด์˜ฌ ๋•Œ๋Š” ๊ผญ exception ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

throw๋˜๋Š” try-catch ์‚ฌ์šฉ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class TryCatch3 {
    public static void main(String[] args) {
        try {
            FileInputStream fs = new FileInputStream("src/chap50_errorexception/test.txt");  //๐Ÿ’ฅException ๋ฐœ์ƒ

            int i;
            while((i=fs.read()) != -1){ //๐Ÿ’ฅException ๋ฐœ์ƒ, file์ฝ์–ด์˜ค๊ธฐ
                System.out.write(i);
            }
        } catch(FileNotFoundException e){ //๐Ÿ’Š
            System.out.println("File not found");
            e.printStackTrace();
        } catch(IOException e){ //๐Ÿ’Š
            System.out.println("Problem while reading");
        }
    }
}

โœ… Exception e

๋ชจ๋“  Exception๋“ค์€ ์ด Exception ์•„๋ž˜์— ์žˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class TryMultiCatch {
    public static void main(String[] args) {
        try{

        }catch(FileNotFoundException e){ //๐Ÿ’Š
            System.out.println("FileNotFoundException");
            e.printStackTrace();
        }catch(ArithmeticException | NullPointerException e ){ //๐Ÿ’Š
            System.out.println("ArithmeticException or NullPointerException");
        }catch(Exception e){
            //๐Ÿ’‰๋ชจ๋“  exception์„ catch
            //์–ด๋–ค exception์ด ๋ฐœ์ƒํ• ์ง€ ์˜ˆ์ƒ์ด ๋˜์ง€ ์•Š์„ ๋•Œ, ์ด˜์ด˜ํ•œ ๊ฑฐ๋ฆ„๋ง
            //๋ชจ๋“  exception์€ ์ด exception์˜ class
            //โ—๏ธ ๋‹จ, ์•ž์—์˜ค๋ฉด ๋ชจ๋“  ์˜ค๋ฅ˜ ์žก์•„๋ฒ„๋ ค ์‹คํ–‰ ์ž์ฒด๊ฐ€ ์•ˆ๋จ
            System.out.println("Exception class catch");
        }
    }
}

โœ… Try-catch finally

return์ด๋“ ,
catch๋กœ ์žก๋“  ๋ชป ์žก๋“ ,
(catchํ–ˆ๋“ , catch ๋ชปํ–ˆ๋“ )
try-catch ์˜์—ญ ์ดํ›„ ๋ฌด์กฐ๊ฑด ์‹คํ–‰

๐Ÿ’ก finally ์ด์œ : ๋ฆฌ์†Œ์Šค๋ฅผ ๋‹ค์‹œ ์ปดํ“จํ„ฐ์—๊ฒŒ ๋Œ๋ ค์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class FinallyCloseTest {
    public static void main(String[] args) throws IOException {
        System.out.println("Main method run start");
        FileInputStream fs= null;
        try {
            //๐Ÿ—‚๏ธ ๋ฆฌ์†Œ์Šค ๋ถˆ๋Ÿฌ์˜ด
            fs = new FileInputStream("src/chap51_trycatch/test.txt");

            int i;
            while((i=fs.read()) != -1){
                System.out.write(i);
            }

            int myInt=10;
            int data= 100/myInt;

            //fs.close(); //๐Ÿ—‚๏ธ ๋ฆฌ์†Œ์Šค ํ•ด์ œ ์ฝ”๋“œ
            //๐Ÿ’ฅ fs๋ฅผ ๋‹ซ์•„์•ผ์ง€ ๋ฆฌ์†Œ์Šค๊ฐ€ ํ•ด์ œ๋˜๋Š”๋ฐ, ๋งŒ์•ฝ ArithmeticException์ด ๋ฐœ์ƒํ•˜๋ฉด ๋ฆฌ์†Œ์Šค๋ฅผ ํ•ด์ œํ•  ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•œ๋‹ค
        } catch(FileNotFoundException e){
            System.out.println("File not found");
            e.printStackTrace();
        } catch(IOException e){
            System.out.println("Problem while reading");
        }catch (ArithmeticException e){
            System.out.println("Cannot be divided by 0");
        }finally { //๋ฆฌ์†Œ์Šค๋Š” ๋ฌด์กฐ๊ฑด ํ•ด์ œํ•˜๋„๋ก finally
            fs.close(); //๐Ÿ—‚๏ธ ๋ฆฌ์†Œ์Šค ํ•ด์ œ ์ฝ”๋“œ, ์–ด๋–ค ๊ฒฝ์šฐ์—๋„ ๋ฆฌ์†Œ์Šค๋Š” ํ•ด์ œํ•˜๋„๋ก
        }
        System.out.println("Main method end");
    }
}

โœ… Try-with-resource, AutoCloseable ์ธํ„ฐํŽ˜์ด์Šค

๐Ÿ’ก Try-with-resource

finally์“ฐ์ง€ ์•Š๊ณ  try์•ˆ์— FileInputStream ๊ฐ€์ ธ์˜ค๋ฉด close๊ฐ€ ์ž๋™์œผ๋กœ ์‹คํ–‰๋จ
Try-with-resource๋Š” AutoCloseable์„ ์ƒ์†ํ•œ class๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค

์œ„์˜ finally์ฝ”๋“œ๋ฅผ Try-with-resource๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ˆ˜์ •

  • Try-with-resource์žˆ์–ด์„œ closeํ•„์š” ์—†์Œ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class FinallyCloseTest {
    public static void main(String[] args) throws IOException {
        System.out.println("Main method run start");
        //๐Ÿ’ก Try-with-resource
        //closeํ•˜๊ณ  ์‹ถ์€ ๋ฆฌ์†Œ์Šค๋ฅผ try()์•ˆ์— ๋„ฃ๋Š”๋‹ค.
        try (FileInputStream fs = new FileInputStream("src/chap51_trycatch/test.txt")){
            int i;
            while((i=fs.read()) != -1){
                System.out.write(i);
            }

            int myInt=10;
            int data= 100/myInt;

        } catch(FileNotFoundException e){
            System.out.println("File not found");
            e.printStackTrace();
        } catch(IOException e){
            System.out.println("Problem while reading");
        }catch (ArithmeticException e){
            System.out.println("Cannot be divided by 0");
        }
        System.out.println("Main method end");
    }
}

โœ… ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฏธ๋ฃจ๊ธฐ: throw

๊ฐ ํ•จ์ˆ˜์—์„œ try, catch๋กœ ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ  mainํ•จ์ˆ˜๋กœ throwํ•ด์„œ ๋ฏธ๋ค„๋ฒ„๋ฆฐ๋‹ค.
โ—๏ธ๋‹จ, mainํ•จ์ˆ˜๋Š” ๊ฐ€์žฅ ์ตœ์ข…์œผ๋กœ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜๋‹ˆ๊นŒ throwํ•  ์ˆ˜ ์—†๋‹ค.
๋งŒ์•ฝ mainํ•จ์ˆ˜๋„ throwํ•ด๋ฒ„๋ฆฌ๋ฉด ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์•ˆ ํ•˜๋Š” ์…ˆ์ด ๋œ๋‹ค.

๋ฐœ์ƒํ•œ ๊ณณ์—์„œ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ , ๋‚˜๋ฅผ ํ˜ธ์ถœํ•œ ๋‹ค๋ฅธ ํ•จ์ˆ˜์—๊ฒŒ throwํ•ด์„œ ์œ„์ž„ํ•˜๊ธฐ
๋ฐœ์ƒ ์ด์œ :์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ•จ์ˆ˜์—์„œ ๋น„์Šทํ•œ exception์ด ๋ฐœ์ƒํ•  ๋•Œ ๊ฐ์ž ํ•จ์ˆ˜์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ,
main method์—์„œ ์ด๋ฅผ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๊ธฐ
๊ฐ ํ•จ์ˆ˜์—์„œ exception ์ฒ˜๋ฆฌํ•˜๋ฉด ๋น„์Šทํ•œ ์ฝ”๋“œ๊ฐ€ ๋ฐ˜๋ณต๋˜๋‹ˆ๊นŒ.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class PushExceptionTest {
    public static void main(String[] args) {

        try {
            printFile("src/chap51_trycatch/test.txt");
            //์•„๊นŒ printFile, getFileStream์—์„œ ๋ฏธ๋ค„๋‘”(throw) ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋ชจ๋‘ ์—ฌ๊ธฐ์„œ ์‹คํ–‰
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e){
            e.printStackTrace();
        }

    }
    //๋˜ ํ•ด๊ฒฐ์„ mainํ•จ์ˆ˜์—๊ฒŒ ๋ฏธ๋ฃจ๊ธฐ
    //throws IOException
    static void printFile(String fileName) throws IOException {
        FileInputStream fs= getFileStream(fileName);
        int i;
        while((i=fs.read()) != -1){
            System.out.write(i);
        }

    }
    //ํŒŒ์ผ ๋ฏธ๋ฃจ๊ธฐ ํ•˜๋Š” ํ•จ์ˆ˜
    //์›ํ•ด๋Š” try, catch๋กœ file๋ถˆ๋Ÿฌ์˜ค๊ธฐ exceptionํ•ด์•ผ ํ•จ
    //throws FileNotFoundException๋กœ ํ•ด๊ฒฐ์„ printFileํ•จ์ˆ˜์—๊ฒŒ ๋ฏธ๋ฃจ๊ธฐ
    static FileInputStream getFileStream(String fileName) throws FileNotFoundException {
        FileInputStream fs = new FileInputStream(fileName);
        return fs;
    }
}

โœ”๏ธ handle exceptions all at once

๋งค์šฐ ๋น„์Šทํ•œ exception์ด ๋ฐœ์ƒํ•˜๋Š” ๋‘ ํ•จ์ˆ˜์—์„œ ๊ฐ๊ฐ exception ์ฒ˜๋ฆฌํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ, main์—์„œ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌ โžก๏ธ ๋” ํšจ์œจ์ ์ธ ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
////๐Ÿ‘Ž๐Ÿป ๊ฑฐ์˜ ๋˜‘๊ฐ™์€ exception์ฒ˜๋ฆฌํ•˜๋Š” ๋น„ํšจ์œจ์  ์ฝ”๋“œ
public class handleExceptionsAtOnce {
    public static void main(String[] args) {

        //txt file, csv file์ฝ์–ด์˜ค๋Š” ํ•จ์ˆ˜
        //๋‘ ํ•จ์ˆ˜ ๋ชจ๋‘ ๋น„์Šทํ•˜๊ฒŒ exception์ฒ˜๋ฆฌํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.
        printTextFile("src/chap51_trycatch/test.txt");
        printCSVFile("src/chap51_trycatch/test.csv");


    }
    //๊ฑฐ์˜ ๋˜‘๊ฐ™์€ exception์ฒ˜๋ฆฌ
    public static void printTextFile(String fileName){
    if(!fileName.contains(".txt")){
        System.out.println("No Text File");
        return;
    }
    try{
        FileInputStream fs= new FileInputStream(fileName);
        int i;
        while((i= fs.read()) != -1){
            System.out.write(i);
        }
    } catch(IOException e){
        System.out.println("IOException");

        }
    }
    //๊ฑฐ์˜ ๋˜‘๊ฐ™์€ exception์ฒ˜๋ฆฌ
    public static void printCSVFile(String fileName){
        if(!fileName.contains(".csv")){
            System.out.println("No CSV File");
            return;
        }
        try{
            FileInputStream fs= new FileInputStream(fileName);
            int i;
            while((i= fs.read()) != -1){
                System.out.write(i);
            }
        } catch(IOException e){
            System.out.println("IOException");

        }
    }
}

๐Ÿ’ก ๋” ํšจ์œจ์ ์ธ ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝ!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//main์œผ๋กœ throw
//main์—์„œ ์ฒ˜๋ฆฌ
public class handleExceptionsAtOnce {
    public static void main(String[] args) {

        try {
            printTextFile("src/chap51_trycatch/test.txt");
            printCSVFile("src/chap51_trycatch/test.csv");
        } catch(IOException e){
            System.out.println("IOException");
        }
    }

    //txt file, csv file์ฝ์–ด์˜ค๋Š” ํ•จ์ˆ˜
    //๋‘ ํ•จ์ˆ˜ ๋ชจ๋‘ ๋น„์Šทํ•˜๊ฒŒ exception์ฒ˜๋ฆฌํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.
    public static void printTextFile(String fileName) throws IOException{
    if(!fileName.contains(".txt")){
        System.out.println("No Text File");
        return;
    }

        FileInputStream fs= new FileInputStream(fileName);
        int i;
        while((i= fs.read()) != -1){
            System.out.write(i);
        }

    }

    public static void printCSVFile(String fileName) throws IOException{
        if(!fileName.contains(".csv")){
            System.out.println("No CSV File");
            return;
        }
            FileInputStream fs= new FileInputStream(fileName);
            int i;
            while((i= fs.read()) != -1){
                System.out.write(i);
            }
    }
}

โœ… ์ง์ ‘ ๋˜์ง€๊ธฐ Check Exception

๋ฌธ์ œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์—์„œ if๋ฌธ์„ ์‚ฌ์šฉํ•ด ๋ฌธ์ œ ์ƒํ™ฉ์„ ์ •์˜ํ•ด๋‘๊ณ 
mainํ•จ์ˆ˜๋กœ exception์„ throwํ•œ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด ์ด ๋ฌธ์ œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ mainํ•จ์ˆ˜์—์„œ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ ๋ฌธ์ œ๊ฐ€ ์…๊ธฐ๋Š” ์ƒํ™ฉ์ด๋ฉด,
๋นจ๊ฐ„์ค„์ด ๋นก!
๊ทธ๋ž˜์„œ runํ•˜๊ธฐ ์ „์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

exception๋„ class์˜ ํ•œ ์ข…๋ฅ˜์ด๊ณ , throwable์„ ์ƒ์†ํ•œ๋‹ค.
if๋ฌธ๊ณผ ํ•จ๊ป˜ ์˜ˆ์™ธ ์žก๊ธฐ
์กฐ๊ฑด๋ฌธ์œผ๋กœ ์žก์•„์„œ, ์ƒ์œ„ ํ•จ์ˆ˜๋กœ throwํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ exception์„ ๋ฏธ๋ฆฌ ์˜ˆ์ƒํ•˜์—ฌ if๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด exception์„ throwํ•˜๋„๋ก ํ•œ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด main method๊ฐ€ ์ด ๋ฌธ์ œ ์ƒ๊ธธ ์—ฌ์ง€ ์žˆ๋Š” method ๋ฌธ์ œ ์žˆ๊ฒŒ ๋ถ€๋ฅด์ž–์•„?
๊ทธ๋Ÿฌ๋ฉด ๋นจ๊ฐ„์ค„์ด ๋”ฑ!!! ์ƒ๊ธด๋‹ค!
๊ทธ๋Ÿฌ๋ฉด main method๊ฐ€ ์ด exception์„ ์žก์•„์„œ try, catch๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด runํ•˜๊ธฐ ์ „์— ๋ฌธ์ œ ์ƒ๊ธด๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋Š”๊ฑฐ์ง€~ โžก๏ธ check Exception
๊ทธ๋Ÿฌ๋ฉด ์—๋Ÿฌ ์—†์ด ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์ง€!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class ThrowUncheckException {
    public static void main(String[] args) {
        System.out.println("main method start");

        //โœ… getIntElement
        int result=0;
        try {
            result = getIntElement(100);
            System.out.println("array result= " + result);
        } catch(Exception e){
            System.out.println("Exception throw of getIntElement is taken care of.");
            e.printStackTrace();
        }

        //โœ… getDivide
        try {
            result= getDivide(0);
            System.out.println("divide result= " + result);
        } catch(Exception e){
            System.out.println("Exception throw of getDivide is taken care of.");
            e.printStackTrace();
        }

        System.out.println("main method end");
    }

    //โœ… getIntElement
    static int getIntElement(int index) throws Exception{
        int[] intArr= {1,2,3,4,5,6,7,8,9,10};

        //์—ฌ๊ธฐ์— ์ง์ ‘ exception ์˜ˆ์ƒํ•ด์„œ if๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌ
        //๋งŒ์•ฝ ๋ฐฐ์—ด ๊ธธ์ด๋ณด๋‹ค ๋” ํฐ ๊ฐ’์„ ๋‹ฌ๋ผ๊ณ  ํ•˜๋ฉด ์–ด๋–กํ•˜์ง€?
        //๋ฏธ๋ฆฌ exception์ฒ˜๋ฆฌํ•ด๋‘์ž!
        //ํ•จ์ˆ˜ ์ด๋ฆ„ ์˜†์— throws Exception์ถ”๊ฐ€
        //์ด๋ ‡๊ฒŒ ํ•˜๋ฉด mainํ•จ์ˆ˜์—์„œ getIntElement ์ž˜๋ชป ํ˜ธ์ถœํ•˜๋ฉด ๋นจ๊ฐ„์ค„ ๋œฌ๋‹ค!
        if(index>= intArr.length){
            throw new Exception("this index does not exist on this array");
        }

        return intArr[index];
    }
    //โœ… getDivide
    static int getDivide(int num) throws Exception{
        if(num == 0){
            throw new Exception("cannot be divided with this number");
        }
        int data= 100/num;;
        return data;
    }
}

โœ… ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ๋˜์ง€๊ธฐ

์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ๋žŒ์˜ ๋‚˜์ด์ธ๋ฐ ์Œ์ˆ˜๊ฐ€ ๋“ค์–ด์˜จ๋‹ค๋ฉด? ๋น„์ฆˆ๋‹ˆ์Šค์˜ ์š”๊ตฌ์— ๋งž๋Š” ์กฐ๊ฑด๋“ค์„ ๋˜์ง€๋„๋ก ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ์กฐ๊ฑด ๋งŒ๋“ค์–ด๋‘๊ณ , ์ด ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜์ง€ ์•Š์œผ๋ฉด Exception๋ฐœ์ƒํ•˜๋„๋ก ํ•˜๊ธฐ

์กฐ๊ฑด

  • PTmemeber class
  • ID๋Š” null์ด ์•„๋‹˜
  • ID๋Š” 8์ž ์ด์ƒ 20์ž ์ดํ•˜
  • height๋Š” ์–‘์ˆ˜
  • weight ์–‘์ˆ˜
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//1๏ธโƒฃ PostiveNumException.java
public class PostitiveNumException extends RuntimeException{
    public PostitiveNumException(String message) {
        super(message);
    }
}

//2๏ธโƒฃ IDFormatException.java
public class IDFormatException extends RuntimeException{
    public IDFormatException(String message) {
        super(message);
    }
}

//3๏ธโƒฃ PTmember.java
//member์— ๋Œ€ํ•œ class
//โญ๏ธ exception ํด๋ž˜์Šค๋“ค import
import chap51_trycatch.Exception.IDFormatException;
import chap51_trycatch.Exception.PostitiveNumException;

public class PTmember {
    private String ID;
    private String name;
    private Integer height;
    private Integer weight;
    private String gender;
//โญ๏ธ ๋ชธ๋ฌด๊ฒŒ, ํ‚ค ์–‘์ˆ˜์—ฌ์•ผ ๋œ๋‹ค๋Š” ์กฐ๊ฑด ์ •์˜
    public PTmember(String name, Integer height, Integer weight, String gender) {
        if(height<0 || weight<0){
            throw new PostitiveNumException("Height or Weight cannot be less than 0");
        }

        this.name = name;
        this.height = height;
        this.weight = weight;
        this.gender = gender;
    }
//โญ๏ธ ID์กฐ๊ฑด ์ •์˜
    public void setID(String ID) {
        if(ID == null){
            throw new IDFormatException("ID cannot be null");
        }
        if(ID.length() <4 || ID.length()>20){
            throw new IDFormatException("ID cannot be shorter than 8 or longer than 20");
        }
        this.ID = ID;
    }

    @Override
    public String toString() {
        return "PTmember{" +
                "ID='" + ID + '\'' +
                ", name='" + name + '\'' +
                ", height=" + height +
                ", weight=" + weight +
                ", gender='" + gender + '\'' +
                '}';
    }
}

//4๏ธโƒฃ Main.java
public class MemeberTest {
    public static void main(String[] args) {
        PTmember member1= new PTmember("Lee", 100, 100, "Female");
        member1.setID("ABCDE");
        System.out.println(member1);
        //PTmember{ID='ABCDEFG', name='Lee', height=100, weight=100, gender='Female'}

        PTmember member2= new PTmember("Jang", -100, -100, "Male");
        member2.setID("ABCDEF");
        System.out.println(member2);
        //Height or Weight cannot be less than 0

        PTmember member3= new PTmember("Kim", 100, 100, "Male");
        member3.setID(null);
        System.out.println(member3);
        //ID cannot be null

        PTmember member4= new PTmember("Park", 100, 100, "Male");
        member4.setID("A");
        System.out.println(member4);
        //ID cannot be shorter than 8 or longer than 20
    }


}



This post is licensed under CC BY 4.0 by the author.