While I realize there is a similar question (How to serialize an Exception object in C#?), and though the answers on that page were helpful, they didn't exactly solve the problem or answer the question posed.
I believe the question was how to serialize the object to allow it to be reconstructed (deserialized) into the same object. I've attempted to use the solution given by davogones and Antony Booth, but without adding the System.Exception
base class on the consuming side (as in: SerializationException: Exception
), it is impossible to use these types (by themselves) as actual exception objects that can be thrown.
Before I continue, let me explain that last statement. I've tried to use Antony Booth's solution in a web service (the service contains the definition for the serializable object) in an attempt to have all consumers use the same exception (hopefully creating a reusable serializable exception type instead of recreating it all over).
Unfortunately, since neither of the types explicitly derive from System.Exception
, you cannot throw
them, which would obviously be useful. Like I mentioned above, it does seem that adding : Exception
to the type class definition on the consuming side allows the object to be thrown, but that requires editing auto-generated WSDL/web service code, which seems intuitively like a bad/non-maintainable practice to me (correct me if I'm wrong).
My first question is, again, is it possible to serialize System.Exception
or to create a derived type that can be serialized, and if it is possible, how would one go about doing such? I should mention that I've looked at what seems the official way to reconstitute the Exception
object, but I'm afraid I don't understand it very well.
My second question is about the architecture of System.Exception
itself. What I would like to know is why the System.Exception
type is marked as [Serializable]
when it has been documented and apparently designed to disallow you from serializing it properly (at least with XML) because it's Data
object implements IDictionary
?
From MSDN:
Q: Why can't I serialize hashtables?
A: The XmlSerializer cannot process classes implementing the IDictionary interface. This was partly due to schedule constraints and partly due to the fact that a hashtable does not have a counterpart in the XSD type system. The only solution is to implement a custom hashtable that does not implement the IDictionary interface.
Given that XML is becoming (if not already is) a new standard for data transportation (officially recommended by Microsoft, nonetheless), it seems absurdly stupid to not allow the only object type in .NET that can be thrown to not be XML-serializable.
I look forward to hearing some thoughts from all the SO'rs (especially since this is my first post).
If you've got questions or need clarification, please don't hesitate to let me know.
Note: I just found this SO post, which seems to answer a few questions, but I guess I'd like to take my own whack at it. let me know if it's too close to a duplicate, though.
Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.
This is because the root class for all exceptions, Throwable implements the Serializable interface. All exceptions by default are serializable and that's a language design decision because the authors wanted exceptions to be capable of being sent across the wire without any special configuration.
You can prevent member variables from being serialized by marking them with the NonSerialized attribute as follows. If possible, make an object that could contain security-sensitive data nonserializable. If the object must be serialized, apply the NonSerialized attribute to specific fields that store sensitive data.
If you're going to serialize a class, it needs a serialVersionID. Your exception classes should always be serializable.
You can create a class derived from Exception
and do the serialization and deserialization yourself by implementing the ISerializable interface.
Example taken from wrox forums, subclassing ApplicationException
:
EDIT: As pointed, ApplicationException
is deprecated. Using the base Exception
class should work just fine.
using System;
using System.Collections;
using System.Runtime.Serialization;
namespace Common.CustomExceptions
{
/// <summary>
/// Custom exception.
/// </summary>
[Serializable]
public class CustomExceptionBase: ApplicationException
{
// Local private members
protected DateTime _dateTime = DateTime.Now;
protected String _machineName = Environment.MachineName;
protected String _exceptionType = "";
private String _exceptionDescription = "";
protected String _stackTrace = "";
protected String _assemblyName = "";
protected String _messageName = "";
protected String _messageId = "";
protected Hashtable _data = null;
protected String _source = "";
protected Int32 _exceptionNumber = 0;
public CustomExceptionBase(): base()
{
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}
public CustomExceptionBase(Int32 exceptionNumber): base()
{
this._exceptionNumber = exceptionNumber;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}
public CustomExceptionBase(Int32 exceptionNumber, String message): base(message)
{
this._exceptionNumber = exceptionNumber;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}
public CustomExceptionBase(Int32 exceptionNumber, String message, Exception innerException):
base(message, innerException)
{
this._exceptionNumber = exceptionNumber;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}
public CustomExceptionBase(Int32 exceptionNumber, String message, Exception innerException, String messageName, String mqMessageId):
base(message, innerException)
{
this._exceptionNumber = exceptionNumber;
this._messageId = mqMessageId;
this._messageName = messageName;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}
public CustomExceptionBase(Int32 exceptionNumber, String message, Exception innerException, String messageName, String mqMessageId, String source):
base(message, innerException)
{
this._exceptionNumber = exceptionNumber;
this._messageId = mqMessageId;
this._messageName = messageName;
this._source = source.Equals("") ? this._source : source;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}
#region ISerializable members
/// <summary>
/// This CTor allows exceptions to be marhalled accross remoting boundaries
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
protected CustomExceptionBase(SerializationInfo info, StreamingContext context) :
base(info,context)
{
this._dateTime = info.GetDateTime("_dateTime");
this._machineName = info.GetString("_machineName");
this._stackTrace = info.GetString("_stackTrace");
this._exceptionType = info.GetString("_exceptionType");
this._assemblyName = info.GetString("_assemblyName");
this._messageName = info.GetString("_messageName");
this._messageId = info.GetString("_messageId");
this._exceptionDescription = info.GetString("_exceptionDescription");
this._data = (Hashtable)info.GetValue("_data", Type.GetType("System.Collections.Hashtable"));
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("_dateTime", this._dateTime);
info.AddValue("_machineName", this._machineName);
info.AddValue("_stackTrace", this._stackTrace);
info.AddValue("_exceptionType", this._exceptionType);
info.AddValue("_assemblyName", this._assemblyName);
info.AddValue("_messageName", this._messageName);
info.AddValue("_messageId", this._messageId);
info.AddValue("_exceptionDescription", this._exceptionDescription);
info.AddValue("_data", this._data, Type.GetType("System.Collections.Hashtable"));
base.GetObjectData (info, context);
}
#endregion
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With