반응형
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace 예외처리
{
// 예외  처리 !!
// 예외 ??  에러??
//ex) 학년을 입력하세요??   :  5  
// 1,2,3,4
 //  예외 ?? 예상하는 결과에서 벗어난 결과 !!
 // 1) 분기문을 이용한 예외처리 !!
/*if( 1 || 2 || 3|| 4)
{
 
}*/
 /*   switch (switch_on)
    {
        default: //  해당하는 케이스가 존재하지 않는 경우 !!
    }
*/
    // try ~ catch ~ finaly  구조화된 예외처리 !!
 
    // 예외는 실행시간에 발생하는 오류이다.
    // try구문은 중첩이 가능하다 !!!
    // 1) 가독성이 떨어진다 !!
    // 2) 프로그램 코드와 예외처리 구문이 함께 공존한다 !!
/*    int Result = 10;
     if( Result == 1)  goto Failed;
     f( Result == 2)  goto Failed;
    if( Result == 3)  goto Failed;
    if( Result == 4)  goto Failed;
 
    Failed :
    switch ()
    {
    //  예외처리구문 !!
        default:
    }
    */
 
 
 
 
 
    class Program
    {
        static void Main(string[] args)
        {
            /*
            int no = 100;
            try
            { // 실제 우리가 구현하고자하는  로직 !! 
                int Result = no / 3;
            }
            catch { //예외 처리 코드 !!
                Console.WriteLine("0으로 값을 나누는 것은 아무 의미가;;");
            }
            */
 
             FileInfo fi = new FileInfo("ABC.txt");
            try
            {
                
                fi.Open(FileMode.Open);
                int len = (int)fi.Length; // 파일의 크기 !!
 
            }
            // 상위타입의 Exception일수록 아래에 구현하자 !!
            catch (IOException ie)
            {
                Console.WriteLine(ie.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            // finally :   try ~catch 구문이 끝난후 반드시 실행되어야하는 코드들이다 !!
            //  Try ~catch에서의 실행결과에 관계 없이 무조건 실해되어야 하는 코드들 !!!
            // catch 블록이 존재하는 finally보다 먼저 실행된다 !!
           // try~catch에서 발생한 리소스들의 정리 작업을  finally에서 수행한다 !!
            finally
            {
                fi.Delete();
            }
        }
    }
}

'C#' 카테고리의 다른 글

[C#] 인덱서  (0) 2014.11.18
[C#] 이벤트  (0) 2014.11.18
[C#] 생성자  (0) 2014.11.18
[C#] 상속  (0) 2014.11.18
[C#] 배열  (0) 2014.11.18

+ Recent posts