투케이2K

50. (C#/NET) [Mac Os] [문법] try catch finally 구문을 사용해 에러 발생 예외 처리 수행 본문

C샵 (NET)

50. (C#/NET) [Mac Os] [문법] try catch finally 구문을 사용해 에러 발생 예외 처리 수행

투케이2K 2024. 3. 15. 15:02
반응형

[개발 환경 설정]

개발 언어 : C# / NET

 

[소스 코드]

using System;
using System.Collections;
using System.Data;

namespace testProject {


    // [프로그램 동작 클래스]
    class Program {

        static void Main(string[] args){
            
            /*
            -------------------------------------------
            [요약 설명]
            -------------------------------------------
            1. try catch finally : 프로그램 동작 중 예외 상황이 발생 했을 때 예외 처리를 수행합니다
            -------------------------------------------
            2. try : 로직 수행 / catch : 에러 발생 / finally : 동작 수행
            -------------------------------------------
            3. throw new Exception : 강제로 예외를 발생할 때 사용합니다
            -------------------------------------------
            */


            // [변수 선언 실시]
            int count = 0;


            // [try catch finally 문을 사용해 예외 처리]
            try {
                Console.WriteLine($"try start : {count}");

                if(count < 10) {
                    throw new Exception("예외 발생"); // [강제 에러 발생]
                }

                Console.WriteLine($"try end : {count}");
            }
            catch(Exception err) {
                Console.WriteLine($"catch : {err.Message}");

                // [변수 값 초기화]
                count = -1;
            }
            finally {
                Console.WriteLine($"finally : {count}");
            }

        }

    }

}
 

[결과 출력]

 

 

반응형
Comments