C#/์ด๊ฒƒ์ด C#์ด๋‹ค

์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•˜๊ธฐ(3) ์˜ˆ์™ธ ๋˜์ง€๊ธฐ

Rainbow๐ŸŒˆCoder 2022. 4. 17. 18:39
728x90

https://youtu.be/Os_o-7eGu7Y

try catch ๋ฌธ์œผ๋กœ ์˜ˆ์™ธ๋ฅผ ๋ฐ›๋Š”๋‹ค๋Š” ๊ฒƒ์€ ์–ด๋””์„ ๊ฐ€ ์˜ˆ์™ธ๋ฅผ ๋˜์ง„๋‹ค๋Š” ์ด์•ผ๊ธฐ์ด๋‹ค.

 

์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

์˜ˆ์™ธ๋Š” throw๋ฌธ์„ ์ด์šฉํ•ด์„œ ๋˜์ง„๋‹ค.

<throw๋ฌธ์œผ๋กœ ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋Š” ์˜ˆ์ œ ์ฝ”๋“œ>

            try
            {
                //...
                throw new Exception("์˜ˆ์™ธ๋ฅผ ๋˜์ง‘๋‹ˆ๋‹ค.");
            }
            catch (Exception e)
            {
                WriteLine(e.Message); //throw ๋ฌธ์„ ํ†ตํ•ด ๋˜์ ธ์ง„ ์˜ˆ์™ธ ๊ฐ์ฒด๋Š” catch๋ฌธ์„ ํ†ตํ•ด ๋ฐ›๋Š”๋‹ค.
            }

<์˜ˆ์ œ์ฝ”๋“œ2> ๋ฉ”์†Œ๋“œ ์•ˆ์—์„œ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด(๋˜๋Š” ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๋ฉด) ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๊ณ , ์ด๋ ‡๊ฒŒ ๋˜์ ธ์ง„ ์˜ˆ์™ธ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” try~catch๋ฌธ์—์„œ ๋ฐ›์•„๋‚ธ๋‹ค.

using System;
using System.Collections;
using static System.Console;

namespace TryCatch
{
    class MainApp
    {
        static void DoSomething(int arg)
        {
            if (arg<10)
                WriteLine("arg:{0}",arg);
            else
                throw new Exception("1111์˜ˆ์™ธ๋ฅผ ๋˜์ง‘๋‹ˆ๋‹ค."); //์˜ˆ์™ธ๋ฅผ ๋˜์กŒ์ง€๋งŒ DoSomething() ๋ฉ”์†Œ๋“œ ์•ˆ์—์„œ๋Š”
                // ์ด ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๊ฐ€ ์—†๋‹ค. ์ด ์˜ˆ์™ธ๋Š” DoSomething() ๋ฉ”์†Œ๋“œ์˜ ํ˜ธ์ถœ์ž์—๊ฒŒ ๋˜์ ธ์ง„๋‹ค.
        }
        
        static void Main(string[] args)
        {
            try
            {
                DoSomething(13); 
            }
            catch (Exception e)// DoSomething() ๋ฉ”์†Œ๋“œ์—์„œ ๋˜์ง„ ํ˜ธ์ถœ์ž์˜ try~catch ๋ธ”๋ก์—์„œ ๋ฐ›๋Š”๋‹ค.
            {
                WriteLine(e.Message); //throw ๋ฌธ์„ ํ†ตํ•ด ๋˜์ ธ์ง„ ์˜ˆ์™ธ ๊ฐ์ฒด๋Š” catch๋ฌธ์„ ํ†ตํ•ด ๋ฐ›๋Š”๋‹ค. //1111์˜ˆ์™ธ๋ฅผ ๋˜์ง‘๋‹ˆ๋‹ค.
            }
        }

    }
}

<์ถœ๋ ฅ>

1111์˜ˆ์™ธ๋ฅผ ๋˜์ง‘๋‹ˆ๋‹ค.

 

 

๋‹ค์Œ์€ throw ๋ฌธ์„ ์ด์šฉํ•˜๋Š” ์˜ˆ์ œํ”„๋กœ๊ทธ๋žจ์ด๋‹ค.

using System;
using System.Collections;
using static System.Console;

namespace TryCatch
{
    class MainApp
    {
        static void DoSomething(int arg)
        {
            if (arg<10)
                WriteLine("arg:{0}",arg);
            else
                throw new Exception($"arg: {arg}, arg๊ฐ€ 10๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค..");
        }
        
        static void Main(string[] args)
        {
            try
            {
                for (int i = 1; i < 15; i += 2) //i๊ฐ€ 13๊นŒ์ง€ ๊ฐ€์•ผํ•˜์ง€๋งŒ 11๋ถ€ํ„ฐ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ 11์—์„œ ๋˜์ง„ ์˜ˆ์™ธ ๋ฐ›์•„์ฃผ๊ณ  13๊นŒ์ง€ ๊ฐ€์ง€๋„ ๋ชปํ•œ ๋ชจ์Šต
                {
                    DoSomething(i);
                }
            }
            catch (Exception e)
            {
                WriteLine(e.Message); //arg: 11, arg๊ฐ€ 10๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค..
            }
        }

    }
}

์ถœ๋ ฅ๊ฒฐ๊ณผ

arg:1
arg:3
arg:5
arg:7
arg:9
arg: 11, arg๊ฐ€ 10๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค..

 

i= 11์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ i= 13 ์ฝ”๋“œ๋Š” ์‹คํ–‰๋˜์ง€๋„ ์•Š๋Š” ๋ชจ์Šต

 

throw๋Š” ๋ณดํ†ต ๋ฌธ์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€๋งŒ c# 7.0๋ถ€ํ„ฐ๋Š” ์‹์œผ๋กœ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐœ์„ ๋˜์—ˆ๋‹ค. 

            int? a = null;
            int b = a ?? throw new ArgumentNullException();

a๋Š” null์ด๋ฏ€๋กœ, b์— a๋ฅผ ํ• ๋‹นํ•˜์ง€ ์•Š๊ณ  throw ์‹์ด ์‹คํ–‰๋œ๋‹ค.

 

throw ์‹์˜ ์˜ˆ์ œ๋ฅผ ํ•˜๋‚˜ ๋” ์‚ดํŽด๋ณธ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์กฐ๊ฑด ์—ฐ์‚ฐ์ž ์•ˆ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

            int[] arr = new[] { 1, 2, 3 };
            int idx = 4;
            int value = arr[
                idx >= 0 && idx < 3
                ? idx : throw new IndexOutOfRangeException()
                ];

 

 

์˜ˆ์ œ

using System;
using System.Collections;
using static System.Console;

namespace TryCatch
{
    class MainApp
    {

        static void Main(string[] args)
        {
            try
            {
                int? a = null;
                int b = a ?? throw new ArgumentNullException();
            }
            catch (ArgumentNullException e) 
            {
                WriteLine(e);
            }

            try
            {
                int[] array = new[] { 1, 2, 3 };
                int index = 4;
                int value = array[index >= 0 && index < 3
                    ? index : throw new IndexOutOfRangeException()];
            }
            catch (IndexOutOfRangeException e)
            {
                WriteLine(e);
            }
        }
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ

System.ArgumentNullException: ๊ฐ’์€ null์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
   ์œ„์น˜: TryCatch.MainApp.Main(String[] args) ํŒŒ์ผ ...\MainApp.cs:์ค„ 15
System.IndexOutOfRangeException: ์ธ๋ฑ์Šค๊ฐ€ ๋ฐฐ์—ด ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ์Šต๋‹ˆ๋‹ค.
   ์œ„์น˜: TryCatch.MainApp.Main(String[] args) ํŒŒ์ผ ...\MainApp.cs:์ค„ 26

728x90