C#/C# Concept

[C#] 예외 처리 finally

군우 2018. 5. 31. 09:55
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace FinallyTest
{
    class FinallyTest
    {
        static void Main(string[] args)
        {
            int[] exArray = new int[2];
            try
            {
                exArray[0= 0;
                exArray[1= 1;
                for (int i = 0; i < exArray.Length; i++)
                {
                    Console.WriteLine("exArray[" + i + "] = " + exArray[i]);
                }
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine(e);
            }
            finally 
            {
                Console.WriteLine("첫번째 finally 문 입니다.");
                Console.WriteLine();
            }
            try
            {
                exArray[2= 2// err
                Console.WriteLine("exArray[2] = " + exArray[2]);
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                Console.WriteLine("두번째 finally 문 입니다. ");
            }
        }
    }
}
 
cs


finally문은   err 발생처리와 상관없이 반드시 처리되는 구문 이다.