예외계층

2026년 3월 30일 09:30분

레위기 20장은 위반마다 결과가 다릅니다. 죄목에 따라 사형이 되거나 공동체에서 끊어집니다. 나는 예외 계층과 처벌 레지스트리를 분리해 설계했습니다. 위반 유형을 등록하고, 실행 시점에 해당 예외를 꺼내 던집니다.

package com.jesusbornd.leviticus;

import java.util.HashMap;
import java.util.Map;

public class Leviticus_20_Chapter_Lv3 {

    static class ViolationException extends RuntimeException {
        private final String code;
        ViolationException(String code, String msg) {
            super(msg);
            this.code = code;
        }
        String code() { return code; }
    }

    static class DeathPenalty extends ViolationException {
        DeathPenalty(String code, String cause) {
            super(code, "사형 / Death: " + cause);
        }
    }

    static class CutOff extends ViolationException {
        CutOff(String code, String cause) {
            super(code, "추방 / Cut off: " + cause);
        }
    }

    static class PenaltyRegistry {
        private final Map<String, ViolationException> registry = new HashMap<>();

        PenaltyRegistry register(String key, ViolationException ex) {
            registry.put(key, ex);
            return this;
        }

        void evaluate(String action) {
            if (registry.containsKey(action)) {
                throw registry.get(action);
            }
            System.out.println("통과 / Pass: " + action);
        }
    }

    public static void main(String[] args) {
        PenaltyRegistry law = new PenaltyRegistry()
            .register("molech",    new DeathPenalty("L20-1", "자녀를 불에 드림"))
            .register("medium",    new CutOff      ("L20-2", "신접한 자 접촉"))
            .register("blasphemy", new DeathPenalty("L20-3", "여호와 이름 저주"));

        String[] actions = {"molech", "medium", "prayer", "blasphemy"};
        for (String a : actions) {
            try {
                law.evaluate(a);
            } catch (ViolationException e) {
                System.out.println("[" + e.code() + "] " + e.getMessage());
            }
        }
    }
}

class ViolationError(Exception):
    def __init__(self, code: str, msg: str):
        super().__init__(msg)
        self.code = code


class DeathPenalty(ViolationError):
    def __init__(self, code: str, cause: str):
        super().__init__(code, f"사형 / Death: {cause}")


class CutOff(ViolationError):
    def __init__(self, code: str, cause: str):
        super().__init__(code, f"추방 / Cut off: {cause}")


class PenaltyRegistry:
    def __init__(self):
        self._registry: dict[str, ViolationError] = {}

    def register(self, key: str, ex: ViolationError) -> "PenaltyRegistry":
        self._registry[key] = ex
        return self

    def evaluate(self, action: str):
        if action in self._registry:
            raise self._registry[action]
        print(f"통과 / Pass: {action}")


if __name__ == "__main__":
    law = (
        PenaltyRegistry()
        .register("molech",    DeathPenalty("L20-1", "자녀를 불에 드림"))
        .register("medium",    CutOff      ("L20-2", "신접한 자 접촉"))
        .register("blasphemy", DeathPenalty("L20-3", "여호와 이름 저주"))
    )

    actions = ["molech", "medium", "prayer", "blasphemy"]
    for a in actions:
        try:
            law.evaluate(a)
        except ViolationError as e:
            print(f"[{e.code}] {e}")

Comments

Avatar
 2026년 3월 30일 23:36분

죄목마다 결과가 다르게 적용되는 구조가, 예외를 계층으로 분리해서 처리하는 설계와 잘 맞닿아 있네요.



Search

← 목록으로