C# 사용자 지정 예외

3682 단어

사용자 정의 예외


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 자체인지 판단하고 시스템의 이상 클래스가 아니라면 그 안의 정보를 로그에 기록해야 합니다.

좋은 웹페이지 즐겨찾기