모름

코드

 

이렇게 커스텀 예외 처리를 작성할 일이 많진 않다고 합니다. 아래는 4개의 8비트 정수를 받아 하나의 32비트 정수 안에 병합하는 MergeARGB() 메소드를 활용한 예외처리입니다.

 

class InvalidArgumentExeption : Exception {
    public InvalidArgumentExeption() {

    }
    public InvalidArgumentExeption(string message) : base(message) {

    }
    public object Argument {
        get;
        set;
    }
    public string Range {
        get;
        set;
    }
}


class Program {
    static uint MergeARGB(uint alpha, uint red, uint green, uint blue) {
        uint[] args = new uint[] { alpha, red, green, blue };

        foreach(uint arg in args) {
            if (arg > 255)
                throw new InvalidArgumentExeption()
                {
                    Argument = arg,
                    Range = "0~255"
                };
        }

        return (alpha << 24 & 0xFF000000) | (red << 16 & 0x00FF0000) | (green << 8 & 0x0000FF00) | (blue & 0x000000FF);
    }

    static void Main(string[] args) {
        try {
            Console.WriteLine("0x{0:X}", MergeARGB(255, 111, 111, 111));
            Console.WriteLine("0x{0:X}", MergeARGB(1, 65, 192, 128));
            Console.WriteLine("0x{0:X}", MergeARGB(0, 255, 255, 300));
        }
        catch (InvalidArgumentExeption e) {
            Console.WriteLine(e.Message);
            Console.WriteLine($"Argument:{e.Argument}, Range:{e.Range}");
        }
    }
}

 

 

 


 

 

 

결과