Files
Ojt.Lectures/ExC.Exception/Program.cs
T
2025-03-05 10:21:10 +09:00

64 lines
1.4 KiB
C#

class MyException : Exception
{
public string MyMessage = string.Empty;
public MyException(string message, Exception ex) :base(ex.Message)
{
MyMessage = message;
}
}
internal class Program
{
static void Step1()
{
throw new IndexOutOfRangeException();
}
static void Step2()
{
throw new FileNotFoundException();
}
static void Step3()
{
throw new Exception();
}
static bool Log(Exception ex)
{
Console.WriteLine($"{DateTime.Now.ToString("yy/MM/dd HH:mm:ss")} - {ex.Message}");
return true;
}
private static void Main(string[] args)
{
try
{
// 실행 문장들
Step1();
Step2();
Step3();
}
catch (IndexOutOfRangeException ex)
{
// 새로운 Exception 생성하여 throw
throw new MyException("Invalid index", ex);
}
catch (FileNotFoundException ex)
{
bool success = Log(ex);
if (!success)
{
// 기존 Exception을 throw
throw ex;
}
}
catch (Exception ex)
{
Log(ex);
// 발생된 Exception을 그대로 호출자에 전달
throw;
}
finally
{
//TODO 사용한 메모리 정리 코드
}
}
}