[백준] C# : A+B - 4 (10951번)

ssu_hyun·2022년 7월 18일
0

Data Structure & Algorithm

목록 보기
35/67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baekjoon
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                string input = Console.ReadLine();
                if (input == null) break;
                string[] n = input.Split();
                int x = int.Parse(n[0]);
                int y = int.Parse(n[1]);
                Console.WriteLine(x+y);
            }
        }
    }
}
// console창에서 예제 입력했을 때 됐던 코드 & 백준에서는 왜 통과안되는지 모르겠는 코드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baekjoon
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                string[] input = Console.ReadLine().Split();
                try
                {
                    int x = int.Parse(input[0]);
                    int y = int.Parse(input[1]);
                    Console.WriteLine(x + y);
                }
                catch  // 에러 발생 시  실행
                {
                    break;
                }
                
            }
        }
    }
}

//2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baekjoon
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                string[] input = Console.ReadLine().Split();
                if (input != null)
                {
                    int x = int.Parse(input[0]);
                    int y = int.Parse(input[1]);
                    Console.WriteLine(x + y);
                }
                else
                {
                    break;
                }
                
            }
        }
    }
}

  • 에러 발생 잡기 : try...catch...
  • 에러 발생시키기 : throw

0개의 댓글