Last active
October 18, 2018 11:45
-
-
Save sandord/443725 to your computer and use it in GitHub Desktop.
Custom exception pattern
This file contains hidden or 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, 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