728x90
1.์๋์ ์ฝ๋๋ฅผ ์ปดํ์ผํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ธ๋ฅผ ํ์ํ๊ณ ๋น์ ์์ ์ผ๋ก ์ข ๋ฃํฉ๋๋ค. try~catch๋ฌธ์ ์ด์ฉํด์ ์์ธ๋ฅผ ์์ ํ๊ฒ ์ก์ ์ฒ๋ฆฌํ๋๋ก ์ฝ๋๋ฅผ ์์ ํ์ธ์.
using System;
namespace Ex12_1
{
class MainApp
{
static void Main()
{
int[] arr = new int[10];
for(int i = 0; i < 10; ++i)
arr[i] = i;
for(int i = 0; i < 11; ++i)
Console.WriteLine(arr[i]);
}
}
}
0
1
2
3
4
5
6
7
8
9
์ฒ๋ฆฌ๋์ง ์์ ์์ธ: System.IndexOutOfRangeException: ์ธ๋ฑ์ค๊ฐ ๋ฐฐ์ด ๋ฒ์๋ฅผ ๋ฒ์ด๋ฌ์ต๋๋ค.
try catch๋ก ๋ฌถ์ด์ฃผ๋ฉด ์์ธ์ฒ๋ฆฌ๋ฅผ ํ ์ ์๋ค.
using System;
using System.Collections.Generic;
using System.Text;
namespace Chapter12.Practice
{
class MainApp
{
static void Main()
{
try
{
int[] arr = new int[10];
for (int i = 0; i < 10; ++i)
{
arr[i] = i;
}
for (int i = 0; i < 11; ++i)
{
Console.WriteLine(arr[i]);
}
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("์์ธ ๋ฐ์: " + e.GetType()+ ": " + e.Message);
}
}
}
}
728x90
'๐ฅ๏ธ > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C#] ๋๋ค์๊ณผ Func, Action ๋๋ฆฌ์ (0) | 2023.09.18 |
---|---|
[C#] ๋๋ฆฌ์(delegate)์ ์ด๋ฒคํธ (0) | 2023.09.10 |
[C#] try catch์ throw๋ฌธ/์ (0) | 2023.08.25 |
[์ด๊ฒ์ด C#์ด๋ค] Chapter11: ์ฐ์ต๋ฌธ์ (0) | 2023.08.22 |
[C#] ์ผ๋ฐํ ํ๋ก๊ทธ๋๋ฐ: ์ปฌ๋ ์ (0) | 2023.08.22 |