Created
August 24, 2020 10:38
-
-
Save wi7a1ian/ae0ae28e80e5ec7ddc279cab66857093 to your computer and use it in GitHub Desktop.
Serializable custom exceptions #csharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Serializable] | |
public class CustomException : Exception | |
{ | |
public CustomException() | |
{ | |
} | |
public CustomException(string message) : base(message) | |
{ | |
} | |
public CustomException(string message, Exception innerException) : base(message, innerException) | |
{ | |
} | |
public CustomException(SerializationInfo info, StreamingContext context) : base(info, context) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Serializable] | |
public class CustomException : Exception | |
{ | |
public ErrorCodes ErrorCode { get; } | |
public CustomException(ErrorCodes errorCode, string message, Exception innerException = null) : base(message, innerException) | |
{ | |
this.ErrorCode = errorCode; | |
} | |
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)] | |
internal CustomException(SerializationInfo info, StreamingContext context) : base(info, context) | |
{ | |
if (info == null) | |
{ | |
throw new ArgumentNullException(nameof(info)); | |
} | |
this.ErrorCode = (ErrorCodes)info.GetInt32(nameof(ErrorCode)); | |
} | |
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)] | |
public override void GetObjectData(SerializationInfo info, StreamingContext context) | |
{ | |
if (info == null) | |
{ | |
throw new ArgumentNullException(nameof(info)); | |
} | |
info.AddValue(nameof(ErrorCode), (int)this.ErrorCode); | |
base.GetObjectData(info, context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment