07.Exception(예외처리) — 웹툰PD 지망생 : 정민재
07.Exception(예외처리)
●/섹션 7. 유용한 문법

07.Exception(예외처리)

728x90

 

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {

        static void Main(string[] args)
        {
            int a = 10;
            int b = 0;
            int result = a / b;

            try
            {
                // 1. 0으로 나눌 때
                // 2. 잘못 된 메모리를 참조 (null)
                // 3. 너무 많은 용량을 복사 (오버플로우)

            }

            catch(Exception e)
            {

            }
        }
    }
}

 

using System;
using System.Collections.Generic;

namespace Csharp
{
    

    class Program
    {

        static void Main(string[] args)
        {            
            try
            {
                // 1. 0으로 나눌 때
                // 2. 잘못 된 메모리를 참조 (null)
                // 3. 너무 많은 용량을 복사 (오버플로우)
                
                int a = 10;
                int b = 0;
                int result = a / b;

            }

            catch(Exception e)
            {

            }
        }
    }
}

이번에는 정상 작동한다.

 

 

첫 번째 방식

using System;
using System.Collections.Generic;

namespace Csharp
{
    

    class Program
    {
        class TestException : Exception
        {

        }

        static void Main(string[] args)
        {            
            try
            {
                // 1. 0으로 나눌 때
                // 2. 잘못 된 메모리를 참조 (null)
                // 3. 너무 많은 용량을 복사 (오버플로우)

                throw new TestException();

            }

            catch(DivideByZeroException e)
            {

            }

            catch(Exception e)
            {

            }
        }
    }
}

 

두 번째 방식

using System;
using System.Collections.Generic;

namespace Csharp
{
    

    class Program
    {
        class TestException : Exception
        {

        }

        static void TestFunc()
        {
            throw new TestException();
        }

        static void Main(string[] args)
        {            
            try
            {
                // 1. 0으로 나눌 때
                // 2. 잘못 된 메모리를 참조 (null)
                // 3. 너무 많은 용량을 복사 (오버플로우)

                TestFunc();
            }

            catch(DivideByZeroException e)
            {

            }

            catch(Exception e)
            {

            }
        }
    }
}

728x90

' > 섹션 7. 유용한 문법' 카테고리의 다른 글

09.Nullable(널러블)  (0) 2021.07.15
08.reflection 리플렉션  (0) 2021.07.15
06.Lambda(람다)  (0) 2021.07.14
05.event(이벤트)  (0) 2021.07.14
04.델리게이트(delegate)  (0) 2021.07.14