Daily Archives: December 24, 2013
Exceptions (part 2, advanced)
Season’s Greetings!
Today we have a very short post about custom exceptions, which provide more specific information. Of course the custom exception has to inherit from System.Exception. Adding several constructors including a parameterless one is good practice.
The suffix “Exception” in your exception name is convention (eg. “OutOfMemory
Exception“, “FileNotFoundException“). Adding the Serializable attribute can be useful when you work across application domains. Properties help providing extra information.
Don’t use the System.ApplicationException class.
http://msdn.microsoft.com/en-us/library/system.applicationexception.aspx states:
If you are designing an application that needs to create its own exceptions, you should derive custom exceptions from the Exception class. It was originally thought that custom exceptions should derive from the ApplicationException class; however in practice this has not been found to add significant value.
In other words: ApplicationException is a relic of the past, where Microsoft intended developers to inherit all their custom exceptions from. But this has never become practice and custom exceptions now derive from the Exception class.
[Serializable] public class UserNotFoundException : Exception { public string UserId { get; private set; } public UserNotFoundException(string xUserId) : base() { UserId = xUserId; base.HelpLink = "http://www.ohta.de"; } // constructor public UserNotFoundException(string xUserId, string xMessage) : base(xMessage) { UserId = xUserId; base.HelpLink = "http://www.ohta.de"; } // constructor public UserNotFoundException(string xUserId, string xMessage, Exception xInnerException) : base(xMessage, xInnerException) { UserId = xUserId; base.HelpLink = "http://www.ohta.de"; } // constructor protected UserNotFoundException(SerializationInfo xSerializationInfo, StreamingContext xStreamingContext) { UserId = xSerializationInfo.GetValue("UserId", typeof(string)) as string; } // constructor public void GetObjectData(SerializationInfo xSerializationInfo, StreamingContext xStreamingContext) { xSerializationInfo.AddValue("UserId", UserId, typeof(string)); } } // class