본문 바로가기

PROGRAMMING LANGUAGE/C#

예외처리 try catch when

using System;

class Program
{
    class FilterableException : Exception
    {
        public int ErrorNo { get; set; }  
    }
      
    static void Main(string[] args)
    {
        Console.WriteLine("Enter Number Between 0~10");
        string input = Console.ReadLine();
        try
        {
            int num = Int32.Parse(input);

            if (num < 0 || num > 10)
                throw new FilterableException() { ErrorNo = num };
            else
                Console.WriteLine($"Output : {num}");
        }
        catch (FilterableException e) when (e.ErrorNo < 0)    //when 필터링
        {
            Console.WriteLine("Negative input is not allowed.");
        }
        catch (FilterableException e) when (e.ErrorNo > 10)
        {
            Console.WriteLine("Too big number is not allowed.");
        }
    }



}

'PROGRAMMING LANGUAGE > C#' 카테고리의 다른 글

델리게이트와 람다식  (0) 2022.03.07
이벤트  (0) 2022.03.04
StringBuilder  (0) 2022.02.22
인덱서  (0) 2022.02.21
string  (0) 2022.01.20