Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
Created August 24, 2020 10:38
Show Gist options
  • Save wi7a1ian/ae0ae28e80e5ec7ddc279cab66857093 to your computer and use it in GitHub Desktop.
Save wi7a1ian/ae0ae28e80e5ec7ddc279cab66857093 to your computer and use it in GitHub Desktop.
Serializable custom exceptions #csharp
[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)
{
}
}
[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