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
'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 |