Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1067879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:12:52+00:00 2026-05-16T20:12:52+00:00

While I realize there is a similar question ( How to serialize an Exception

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:12:52+00:00Added an answer on May 16, 2026 at 8:12 pm

    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
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.