[개발] - Java/후발대

후발대 19일차 전체 코드

완벽한 장면 2023. 2. 24. 01:13

후발대수업_19.

예외처리2 (Throw, Finally, TryWithResources, CustomException, Throws)

진행 내용 (수업자료)

실습코드

Stream 퀴즈풀이

package com.sparta.hbd04.prac01.prac15;

// 메리 50
// 나나 42
// 벤자민 21
// 코코 18
// 마이클 5

import java.util.ArrayList;

// 미술관 입장료
// -----------
// 메리 50 5000원
// 나나 42 5000원
// 벤자민 21 5000원
// 코코 18 무료
// 마이클 5  무료
public class Prac15 {
    public static void main(String[] args) {
        ArrayList<Customer> list = new ArrayList<>();
        list.add(new Customer("메리",50));
        list.add(new Customer("나나",42));
        list.add(new Customer("벤자민",21));
        list.add(new Customer("코코",18));
        list.add(new Customer("마이클",5));

        System.out.println("미술관 입장료");
        System.out.println("--------------");

        list.stream()
                .map(x -> x.age >= 20 ? x.name + " 5000원" : x.name + " 무료")
                .forEach(System.out::println);

    }
}

class Customer {
    public String name;
    public int age;

    public Customer(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

 

_03_Throw

package com.sparta.hbd04.prac18;

public class _03_Throw {
    public static void main(String[] args) {
        int age = 17; //만 17세
        try {
//            System.out.println(3 / 0);
            if (age < 19) {
//                System.out.println("만 19세 미만에게는 판매하지 않습니다.");
                throw new Exception("만 19세 미만에게는 판매하지 않습니다.");
            } else {
                System.out.println("주문하신 상품 여기 있습니다.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

_04_Finally

package com.sparta.hbd04.prac18;

public class _04_Finally {
    public static void main(String[] args) {
        try {
            System.out.println("택시의 문을 연다.");
//            throw new Exception("휴무 택시");
            System.out.println("택시에 탑승한다.");
        } catch (Exception e) {
            System.out.println("!! 문제 발생: " + e.getMessage());
        } finally {
            System.out.println("택시의 문을 닫는다.");
        }

        // try + catch(s)
        // try + catch(s) + finally
        // try + finally

        System.out.println("-------------");

        try {
            System.out.println(3/0);
        } finally {
            System.out.println("프로그램 정상 종료");
        }
    }
}

 

05_TryWithResources

package com.sparta.hbd04.prac18;

import java.io.BufferedWriter;

public class _05_TryWrithResources {
    public static void main(String[] args) {
        MyFileWriter writer1 = null;
        try {
            writer1 = new MyFileWriter();
            writer1.write("아이스크림이 먹고 싶어요.");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                writer1.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        System.out.println("-------------");

        try (MyFileWriter writer2 = new MyFileWriter()) {
            writer2.write("빵이 먹고 싶어요.");
        } catch (Exception e) {
            e.printStackTrace();
        }

        BufferedWriter bw = null;
    }
}

class MyFileWriter implements AutoCloseable {

    @Override
    public void close() throws Exception {
        System.out.println("파일을 정상적으로 닫습니다.");
    }

    public void write(String line) {
        System.out.println("파일에 내용을 입력합니다.");
        System.out.println("입력 내용 : " +line);
    }
}
728x90
반응형