Skip to content

Instantly share code, notes, and snippets.

@albertospelta
Created October 16, 2018 10:47
Show Gist options
  • Save albertospelta/3bcf282cb5e70cec907253af29074c27 to your computer and use it in GitHub Desktop.
Save albertospelta/3bcf282cb5e70cec907253af29074c27 to your computer and use it in GitHub Desktop.
Template for creating custom exceptions
using System;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace App.Infrastructure.Exceptions
{
[Serializable]
public class SampleException: Exception, ISerializable
{
public string CustomErrorProperty { get; set; }
public SampleException()
{
}
public SampleException(string message)
: base(message)
{
}
public SampleException(string message, Exception innerException)
: base(message, innerException)
{
}
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.SerializationFormatter)]
protected SampleException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
CustomErrorProperty = info.GetString(nameof(CustomErrorProperty));
}
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.SerializationFormatter)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(CustomErrorProperty), CustomErrorProperty);
base.GetObjectData(info, context);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment