728x90
반응형
728x170
■ Exception 클래스를 사용해 전체 예외 메시지를 구하는 방법을 보여준다.
▶ 예제 코드 (C#)
#region 전체 예외 메시지 구하기 - GetFullExceptionMessage(exception)
/// <summary>
/// 전체 예외 메시지 구하기
/// </summary>
/// <param name="exception">예외</param>
/// <returns>전체 예외 메시지</returns>
public string GetFullExceptionMessage(Exception exception)
{
IEnumerable<Exception> exceptionEnumerable = GetAllExceptionEnumerable(exception);
IEnumerable<string> messageEnumerable = exceptionEnumerable.Where(e => !string.IsNullOrWhiteSpace(e.Message))
.Select(e => e.Message.Trim());
string totalMessage = string.Join(Environment.NewLine, messageEnumerable);
return totalMessage;
}
#endregion
#region 모든 예외 열거 가능형 구하기 - GetAllExceptionEnumerable(exception)
/// <summary>
/// 모든 예외 열거 가능형 구하기
/// </summary>
/// <param name="exception">예외</param>
/// <returns>모든 예외 열거 가능형</returns>
private IEnumerable<Exception> GetAllExceptionEnumerable(Exception exception)
{
yield return exception;
if(exception is AggregateException aggregateException)
{
foreach(Exception innerEx in aggregateException.InnerExceptions.SelectMany(e => GetAllExceptionEnumerable(e)))
{
yield return innerEx;
}
}
else if(exception.InnerException != null)
{
foreach(Exception innerException in GetAllExceptionEnumerable(exception.InnerException))
{
yield return innerException;
}
}
}
#endregion
728x90
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON/.NET6] Process 클래스 : GetProcessesByName 정적 메소드를 사용해 프로세스 실행 여부 구하기 (0) | 2022.10.02 |
---|---|
[C#/COMMON/.NET6] Regex 클래스 : Replace 메소드를 사용해 숫자 문자가 아닌 경우 제거하기 (0) | 2022.10.02 |
[C#/COMMON/.NET6] DateTime 구조체 : 날짜/시간에서 UNIX 시간 구하기 (0) | 2022.10.02 |
[C#/COMMON/.NET6] DateTime 구조체 : 날짜/시간 (표준시)에서 UNIX 시간 구하기 (0) | 2022.10.02 |
[C#/COMMON/.NET6] DateTime 구조체 : UNIX 시간에서 날짜/시간 (표준시) 구하기 (0) | 2022.10.02 |
[C#/COMMON/.NET6] Exception 클래스 : 모든 예외 열거 가능형 구하기 (0) | 2022.10.02 |
[C#/COMMON/.NET6] Exception 클래스 : InnerException/Message 속성을 사용해 전체 예외 메시지 구하기 (0) | 2022.10.02 |
[C#/COMMON/.NET6] Exception 클래스 : GetBaseException 메소드를 사용해 최초 내부 예외 구하기 (0) | 2022.10.02 |
[C#/COMMON/.NET6] Exception 클래스 : InnerException 속성을 사용해 최초 내부 예외 구하기 (0) | 2022.10.02 |
[C#/COMMON/.NET6] URL 인코딩 사용하기 (0) | 2022.10.02 |
댓글을 달아 주세요