2일차 구구단 중에 원하는 단을 입력해주세요. FormatException 에러

Console Programming/C# Console 2019. 9. 19. 14:39

구구단 중에 원하는 단을 입력받고, 그 단을 출력하는 프로그램이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax02
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Console.Write("구구단중에 원하는 단을 입력해주세요. ");
            string input = Console.ReadLine();
               
            int num = Convert.ToInt32(input);
              
            for (int i = 1; i < 10; i++)
            {
                Console.WriteLine($"{num} x {i} = {num * i}");
               
            }
 
            
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 입력받은 문자열을 Convet.ToInt32를 이용하여 정수로 바꿨다.

 

실행결과

숫자를 입력하면 잘 돌아간다. 그러나.........
숫자가 아닌 값을 입력하면 이런 에러가 발생한다.

 

디버그를 눌러보면 다음과 같은 화면이 나타난다.

 

입력된 문자열의 형식이 잘못되었을 때의 예외를 처리하지 않았다.

: