본문 바로가기
Study/C#

[C#][Study][기초다지기] throw 문

by 스테디코디스트 2023. 9. 23.
반응형

throw 문

- 예외를 처리할 때 사용

- 예외는 catch 블록에서 처리됨

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace throw_statement
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2, result;
 
            Console.WriteLine("Enter First Number");
            num1 = Convert.ToInt32(Console.ReadLine());
 
            Console.WriteLine("Enter Second Number");
            num2 = Convert.ToInt32(Console.ReadLine());
 
            try
            {
                if (num2 == 0)
                {
                    throw new Exception("Can't Divide by Zero Exception\n\n");
                }
                result = num1 / num2;
                Console.WriteLine("{0} / {1} = {2}", num1, num2, result);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error : {0}", e.ToString());
            }
            Console.ReadLine();
        }
    }
}