C#

C# - 예외처리

마루설아 2024. 12. 10. 20:43
using System;
using System.Collections;

class Program
{
     static void Main(String[] args)
     {
          double num = 100.0;
          int num2;

          Console.Write("숫자 입력 => ");

          try
          {
               num2 = int.Parse(Console.ReadLine());
               Console.WriteLine(num / num2);
          } catch(Exception e)
          {
               Console.WriteLine("예외 : " + e.Message);
          }
          
     }
}

 

 

0 나누기 예외를 연출하려했지만 무한대가 나왔다.

 

 

대신 int형 변수에 소수를 줬더니 예외로 처리되었다.