Serialize Custom Exceptions
Learn what to do when .NET’s built-in exception classes just don’t cut it.
October 30, 2009
Troubleshooting Tips and Tricks
LANGUAGES:VB .NET
TECHNOLOGIES:Custom Exceptions | Serialization
Serialize Custom Exceptions
Learn what to do when .NET's built-in exception classesjust don't cut it.
By Don Kiely
In an upcoming article for asp.netPRO magazine, I cover creating and using custom exceptionclasses you can use when the built-in .NET exception classes simply aren'tenough. Custom exceptions come in handy when none of the .NET exception classesdescribe what went wrong, or if you want to add additional information aboutthe problem beyond a simple message string.
The base exception class, System.Exception, includes fourconstructors, any or all of which you can override in your custom exceptionclass. Depending on how you'll use the custom exception, normally you alwaysoverride the first three:
The default constructor that takes no parameters andsimply calls the base class's constructor
A constructor that takes a string parameter to includea custom message string
A constructor that takes a custom message string andanother exception object that you then can access through the customexception's InnerException property
The fourth constructor, serialization, is optional, butit's required if you are going to support serialization. Serialization lets youtake an object and convert it to a stream of bytes that are useful forstreaming to a disk file, converting to XML, or passing as an array of bytes toanother component. The underlying exception objects are serializable already,but you must provide support for any custom properties you implement as part ofthe custom exception.
In the scenario I used for the code, the customaspnetPROException class carried along with it information about theapplication, the environment in which it was running, and some contactinformation for the developer. By including this information and providingsupport for serializing the exception, you can stream the object directly tosome store - such as the Windows event log - and all information is carried alongin a complete unit. Doing things this way means you don't have to addadditional info to the event log programmatically; you simply stream it thereand the developer has all the information needed to resolve the problem.
Here's how the serialization constructor looks. Thisconstructor takes two parameters, SerializationInfo and StreamingContextobjects that .NET passes to the constructor automatically. Both are part of theSystem.Runtime.Serialization namespace in the .NET Framework:
Protected Sub New(ByVal info As SerializationInfo, _
ByVal context AsStreamingContext)
'Probably won't be usedmuch, but this constructor accepts
'serialized data torehydrate an exception object
MyBase.New(info,context)
msAppName =info.GetString("msAppName")
msPlainDesc =info.GetString("msPlainDesc")
msDevEmail =info.GetString("msDevEmail")
msDevPhone =info.GetString("msDevPhone")
mdOccurred =info.GetDateTime("mdOccurred")
msMachineName =info.GetString("msMachineName")
msAppDomain =info.GetString("msAppDomain")
End Sub
This version of the constructor starts by calling theconstructor of the underlying base class, just like the other constructors. Theparameters passed into this constructor are info and context. info provides information about theinterface members of the class - those that need to be serialized ordeserialized. context describes thesource and destination of the serialization stream.
The rest of the code in the constructor links the class'sprivate variables to how they are serialized. Each line reads the value of akey from the serialized data and assigns that value to the appropriate modulevariable. This is how the custom properties of the custom exception are"rehydrated."
The flip side of this serialization constructor is theGetObjectData method. To serialize an object's data properly, you need tooverride this method of the base class and write the values of your customproperties:
Public Overrides Sub GetObjectData(ByVal info As SerializationInfo,_
ByVal context AsStreamingContext)
info.AddValue("msAppName", msAppName, GetType(String))
info.AddValue("msPlainDesc", msPlainDesc, GetType(String))
info.AddValue("msDevEmail", msDevEmail, GetType(String))
info.AddValue("msDevPhone", msDevPhone, GetType(String))
info.AddValue("mdOccurred", mdOccurred, GetType(Date))
info.AddValue("msMachineName", msMachineName, GetType(String))
info.AddValue("msAppDomain", msAppDomain, GetType(String))
MyBase.GetObjectData(info, context)
End Sub
This code adds the value of each custom property in turn,identifying the type of data with GetType. The last line of code calls the baseclass's GetObjectData method, passing in the info object that now contains boththe standard properties of an exception object and our custom properties.
By overriding the serialization constructor and theGetObjectData method of the base System.Exception class, you can serialize yourcustom exception object to almost any kind of data store. And you can takeadvantage of all the serialization support in .NET to handle any kind of data,including binary data.
Don Kiely is senior technology consultant forInformation Insights, a business and technology consultancy in Fairbanks,Alaska. E-mail him at mailto:[email protected].
About the Author
You May Also Like