C# 사용자 지정 예외
사용자 정의 예외
C#에는 다양한 예외 클래스가 제공되지만 일부 다른 요구 사항을 충족하기 위해 예외 클래스를 사용자화할 수 있습니다.오늘 우리가 정의한 이 이상 클래스에 추가된 기능도 간단합니다. 이상 정보를 로그에 출력하면 됩니다.
그래서 코드는 다음과 같다.
[Serializable]
public class PFTException : Exception
{
public PFTException()
{
}
public PFTException(string message)
: base(message)
{
PFTLog.Error(message, () => { });
}
public PFTException(string messageFormat, params object[] args)
: base(string.Format(messageFormat, args))
{
PFTLog.Error(string.Format(messageFormat, args), () => { });
}
public PFTException(string message, Exception innerException)
: base(message, innerException)
{
// Exception
if (!(innerException is PFTException))
{
PFTLog.Error(message, innerException, () => { });
}
}
///
/// ISerialization 。
///
///
///
private PFTException(SerializationInfo info, StreamingContext context) : base(info, context)
{
// stringInfo = info.GetString("StringInfo");
}
///
/// GetObjectData 。 , GetObjectData
///
///
///
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
//
//info.AddValue("StringInfo", stringInfo);
// ,
base.GetObjectData(info, context);
}
}
안의 로그 방법은 저의 이전 [Log4Net 로그 기록의 실현]을 볼 수 있습니다. 우리가 PFTException 정보를 직접 던질 때 메시지 정보를 로그에 저장합니다. 만약에 Exception이 있다면 Exception이 PFTException 자체인지 판단하고 시스템의 이상 클래스가 아니라면 그 안의 정보를 로그에 기록해야 합니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.