CHAPTER 7 UNDERSTANDING STRUCTURED EXCEPTION HANDLING
•
Defines a default constructor
•
Defines a constructor that sets the inherited Message property
•
Defines a constructor to handle “inner exceptions”
•
Defines a constructor to handle the serialization of your type
Now, based on your current background with .NET, you might have no experience regarding the
role of attributes or object serialization, which is just fine. I’ll address these topics later in the text (see
Chapter 15 for information on attributes and Chapter 20 for details on serialization services). However,
to complete our examination of building custom exceptions, here is the final iteration of
CarIsDeadException, which accounts for each of these special constructors (the other custom properties
and constructors would be as seen in the example found in “Building Custom Exceptions, Take Two”):
[Serializable]
public class CarIsDeadException : ApplicationException
{
public CarIsDeadException() { }
public CarIsDeadException(string message) : base( message ) { }
public CarIsDeadException(string message,
System.Exception inner)
: base( message, inner ) { }
protected CarIsDeadException(
System.Runtime.Serialization.Ser ializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base( info, context ) { }
}
// Any additional custom properties, constructors and data members...
Given that building custom exceptions that adhere to .NET best practices really differ by only their
name, you will be happy to know that Visual Studio provides a code snippet template named
“Exception” (see Figure 7-1) that will autogenerate a new exception class that adheres to .NET best
practices. (Recall from Chapter 2, a code snippet can be activated by typing its name [exception, in this
case] and pressing the Tab key twice.)
270