예외처리(Exception Handing)
       -예외(Exception)
: 예상하지 못했던 상황
       - 형변환
       - 파일처리
       - DB처리 등..
예외 클래스(객체)
       -System.Exception : 루트 클래스
       -일반적으로 "예외명" + "Exception"
       -입출력 예외 :
IOEXception
       -배열 인덱스 예외 :
IndexOutOfRangeException
       -오버플로우 예외 :
OverflowException
try-catch
문
       - 발생하는 예외를  처리하는 구문
       - try : 예외가 발생하는 구문을 
       - csatch : 예외 발생 시 처리하는 구문을 갖는다.
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleEx
{
       //Ex111_Exception.cs
       class Ex111_Exception
       {
             static void Main(string[]
args)
             {
                    //
                    Console.Write("숫자 입력(0은 제외) : ");
                    int num = int.Parse(Console.ReadLine());
                    if (num != 0)
                    {
                           //비즈니스 코드(실제 업무 관련 코드)
                           Console.WriteLine("10
/ {0} = {1}", num, 10 / num);
                    }
                    else
                    {
                           //예외 코드
                           Console.WriteLine("0은 입력 불가능!!");
                    }
                    //try - catch문
                    try
                    {
                           //비즈니스 코드(예외 발생 가능성 있는)
                           Console.WriteLine("10
/ {0} = {1}", num, 10 / num);
                           Console.WriteLine("테스트");
                    }
                    catch (Exception
e)
                    {
                           //예외 처리 코드
                           Console.WriteLine("0은 입력 불가능!!");
                           //Console.WriteLine(e.ToString());
                           Console.WriteLine(e.Message);
                           Console.WriteLine(e.Source);
                    }
             }
       }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleEx
{
       //Ex112_Exception.cs
       class Ex112_Exception
       {
             static void Main(string[]
args)
             {
                    try
                    {
                           int n = 0;
                           Console.WriteLine(100
/ n);//김대리
                           //new
DivideByZeroException() 실행
                           //인스턴스를 던지기!!던진것을 캐치가 잡는다.
                           byte b;
                           short s =
30;
                           b = checked((byte)s);//Overflow, 이부장
                           Console.WriteLine(b);
                           int[] ns =
{ 100, 200, 300 };
                           Console.WriteLine(ns[5]);//박과장
                    }
                    //■----------------종류별예외
시작---------------------
                    catch (DivideByZeroException e)
                    {
                           Console.WriteLine("김대리에게 연락");
                    }
                    catch (OverflowException e)
                    {
                           Console.WriteLine("이부장에게 연락");
                    }
                    catch (IndexOutOfRangeException e)
                    {
                           Console.WriteLine("박과장에게 연락");
                    }
                    //-----------------종류별예외 끝--------------------■
                    //C#상의 모든 예외사항 최상위 객체
                    catch (Exception e)
                    {
                           Console.WriteLine(e.Message);
                    }
                    //☆다중 예외 처리시 제일 부모격인 Exception이
아래에 배치하자.
                    finally
                    {
                           //try를 실행하고 나서..
                           //catch를 실행하고 나서..
                           //항상 실행되는 출력
                           //Clean-up 코드(자원 해제 코드)
                    }
             }
       }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
       class Class1
       {
             static void Main(string[]
args)
             {
                    //예외?
                    // - 닷넷에서 정해진 예외
                    //요구사항] 짝수만 입력!!
                    //홀수입력 -> 잘못된 입력 -> 예외
                    int n = 1;
                    //if(n % 2 ==0)
                    //     Console.WriteLine("업무 진행..");
                    //else
                    //     Console.WriteLine("예외 처리..");
                    try
                    {
                           //예외 발생 코드 존재?
                           //n ? 홀수
                           //예외 던지기
                           //100 / 0 ->
New Divide..(); -> 던지기
                           //Exception e =
new Exception();
                           //throw e;
                           if (n % 2
== 1)
                                 //메시지를 직접 던질수 있다. 
                                 throw
new Exception("홀수입력");
                    }
                    catch (Exception e)
                    {
                           Console.WriteLine("예외처리..");
                           Console.WriteLine(e.Message);
                    }
                    M1();
             }
             private static void M1()
             {
                    throw new NotImplementedException();
             }
       }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
       class Exception04
       {
             static void Main(string[]
args)
             {
                    //상황] 온라인 쇼핑몰..
                    //준비] 예외 리스트 작성
                    //예외코드 작성
                    //결제오류(카드) : A001
                    //결제오류(현금) : A002
                    //배송오류 : B001
                    //상품불량 : C001
                    try
                    {
                           //업무 진행..
                           //상황 발생 -> 예외 발생!!
                           //if(조건)
                           throw new ShopException(ErrorCode.C001);
                    }
                    catch
(ShopException e)
                    {
                           Console.WriteLine(e.ToString());
                    }
             }
       }
       //1. 열거형 선언(제시한 값외에는 사용불가!!)
       enum ErrorCode
{ A001, A002, B001, C001 }
       //2. 사용자 예외 클래스 선언
       //Exception 상속 -> try-catch에 적용시킬수있음!!
       //catch(ShopException e)
       class ShopException
: Exception
       {
             //예외 구분 -> 예외처리 코드 변수 -> A001, B001
             private ErrorCode code;
             //생성자
             public
ShopException(ErrorCode code)
             {
                    this.code = code;
             }
             //프로퍼티
             public ErrorCode Code
             {
                    get
                    { return this.code; }
             }
             public override string
ToString()
             {
                    if (this.code == ErrorCode.A001)
                    {
                           return "카드 결제 오류 -> 내선 번호 100으로 연락";
                    }
                    else if (this.code ==
ErrorCode.A002)
                           return "현금 결제 오류 -> 내선 번호 101 연락";
                    else if (this.code ==
ErrorCode.B001)
                           return "배송오류 -> 내선 번호 200 연락";
                    else if (this.code ==
ErrorCode.C001)
                           return "배송오류 -> 내선 번호 300 연락";
                    else
                           return "일반오류 -> 내선번호 000 연락";
             }
       }
}