[C#/COMMON/.NET6] Exception 클래스 : StackTrace 속성을 사용해 예외 발생 소스 코드 줄 번호 구하기
C#/Common 2022. 6. 21. 00:03728x90
728x170
■ Exception 클래스의 StackTrace 속성을 사용해 예외 발생 소스 코드의 줄 번호를 구하는 방법을 보여준다.
▶ Exception 클래스 : StackTrace 속성을 사용해 예외 발생 소스 코드 줄 번호 구하기 예제 (C#)
try
{
throw new Exception();
}
catch(Exception exception)
{
int line = GetLineNumber(exception);
Console.WriteLine($"ERROR LINE : {line}");
}
▶ Exception 클래스 : StackTrace 속성을 사용해 예외 발생 소스 코드 줄 번호 구하기 (C#)
#region 줄 번호 구하기 - GetLineNumber(exception)
/// <summary>
/// 줄 번호 구하기
/// </summary>
/// <param name="exception">예외</param>
/// <returns>줄 번호</returns>
public int GetLineNumber(Exception exception)
{
int lineNumber = 0;
const string lineSearch = ":line ";
int index = exception.StackTrace.LastIndexOf(lineSearch);
if(index != -1)
{
string lineNumberText = exception.StackTrace.Substring(index + lineSearch.Length);
if(int.TryParse(lineNumberText, out lineNumber))
{
}
}
return lineNumber;
}
#endregion
728x90
그리드형(광고전용)