Skip to content

Instantly share code, notes, and snippets.

@sandord
Last active October 18, 2018 11:45
Show Gist options
  • Save sandord/443725 to your computer and use it in GitHub Desktop.
Save sandord/443725 to your computer and use it in GitHub Desktop.
Custom exception pattern
[Serializable]
public class CustomException : Exception
{
public CustomException() { }
public CustomException(string message) : base(message) { }
public CustomException(string message, System.Exception innerException) : base(message, innerException) { }
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
//...
base.GetObjectData(info, context);
}
protected CustomException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
[Serializable]
public class CustomException : Exception
{
private const string DefaultMessage = "Custom exception message.";
private const string DefaultMessageWithId = "Custom exception message [{0}].";
public CustomException() : this(DefaultMessage) { }
public CustomException(int id) : this(string.Format(CultureInfo.InvariantCulture, DefaultMessageWithId, id)) { }
public CustomException(string message) : base(message ?? DefaultMessage) { }
public CustomException(string message, Exception innerException) : base(message ?? DefaultMessage, innerException) { }
protected CustomException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment