250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • MySQL
  • ์ปดํฌ์ง€์…˜
  • ์œ„์ž„
  • ใ…ฃใ„ท

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•˜๊ธฐ(3) ์˜ˆ์™ธ ๋˜์ง€๊ธฐ
C#/์ด๊ฒƒ์ด C#์ด๋‹ค

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

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

'C# > ์ด๊ฒƒ์ด C#์ด๋‹ค' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๋Œ€๋ฆฌ์ž์™€ ์ต๋ช…๋ฉ”์†Œ๋“œ(1)  (0) 2022.11.28
์˜ˆ์™ธ์ฒ˜๋ฆฌํ•˜๊ธฐ(4) try~catch์™€ finally  (0) 2022.04.18
์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•˜๊ธฐ(2) System.Exception ํด๋ž˜์Šค  (0) 2022.04.17
60. ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•˜๊ธฐ(1)  (0) 2022.04.09
59. foreach๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ผ๋ฐ˜ํ™” ํด๋ž˜์Šค  (0) 2022.02.28
    'C#/์ด๊ฒƒ์ด C#์ด๋‹ค' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • ๋Œ€๋ฆฌ์ž์™€ ์ต๋ช…๋ฉ”์†Œ๋“œ(1)
    • ์˜ˆ์™ธ์ฒ˜๋ฆฌํ•˜๊ธฐ(4) try~catch์™€ finally
    • ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•˜๊ธฐ(2) System.Exception ํด๋ž˜์Šค
    • 60. ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•˜๊ธฐ(1)
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”