반응형
뇌를자극하는 C#.md

뇌를 자극하는 C# 5.0 프로그래밍 Chapter12 연습문제


1. 아래의 코드를 컴파일하고 실행하면 다음과 같이 예외를 표시하고 비정상적으로 종료합니다. try~catch 문을 이용해서 예외를 안전하게 잡아 처리하도록 코드를 수정하세요.


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
using System;
 
namespace Ex12_1
{
    class MainApp
    {
        static void Main(string[] args)
        {
            int[] arr = new int[10];
 
            for (int i = 0; i < 10; i++)
                arr[i] = i;
 
            for (int i = 0; i < 11; i++)
                try
                {
                    Console.WriteLine(arr[i]);
                }
                catch(IndexOutOfRangeException e)
                {
                    Console.WriteLine("에러 : " + e.Message);
                }
        }
    }
}
cs


출력결과




반응형

+ Recent posts