2

I need to serialize and exception, i created a Serializable class

[Serializable]
public class MessageException : Exception
{
    public string Exception
    { get; set; }
    public MessageException()
    { }
    public MessageException(System.Exception ex) 
    {
        this.Exception = ex.Message.ToString();
    }
}

and i try to call the class from the following exception when it occur

catch (Exception ex)
        {
            MessageException exception = new MessageException(ex);
            var exSerializer = new XmlSerializer(typeof(MessageException));


            var writer = new StringWriter();
            exSerializer.Serialize(writer, exception);
            writer.Close();
            Compair2Files(writer.ToString(), BaseString);
        }

it fails on the second row "var exSerializer = new XmlSerializer(typeof(MessageException));"

What I m actually trying to do is when I catch the exception I want to serialized it and then store it into a file for my unit test program. Would you be able to help me thanks in advance Jp

Maslow
  • 18,464
  • 20
  • 106
  • 193
jprbest
  • 717
  • 6
  • 15
  • 32
  • possible duplicate of [How to serialize an Exception object in C#?](http://stackoverflow.com/questions/486460/how-to-serialize-an-exception-object-in-c) –  Jan 26 '11 at 20:29

1 Answers1

1

You cannot serialize exceptions using XmlSerializer.
You need to use binary serialization.

The base Exception class is already [Serializale], so your class is useless.
It's also wrong; you're ignoring the stack trace.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • ok now my question if i use the binary i need to use stream, can store the stream into a string instead of a file?? mean Stream stream = (something here that will allow me to store it into string) can i use Memorystream ?? – jprbest Jan 26 '11 at 20:48
  • No. There is no way to serialize an exception to a string. Instead, you can just call `ex.ToString()`, which will return relevant information. – SLaks Jan 26 '11 at 20:51
  • You _can_ store the binary data in-memory using `MemoryStream`. – SLaks Jan 26 '11 at 20:51
  • here my code: now catch (Exception ex) { MemoryStream mStream = new MemoryStream(); BinaryFormatter bFormatter = new BinaryFormatter(); bFormatter.Serialize(mStream, ex); Compair2Files(mStream.ToString(), BaseString); } got a error sayiing SerializationException was unhandled, any idea – jprbest Jan 26 '11 at 20:59
  • Also, `mStream.ToString()` is meaningless. There is no way to get a human-readable string. – SLaks Jan 26 '11 at 21:02
  • Type 'Messages.MessageDefinitionException' in Assembly 'Messages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. – jprbest Jan 26 '11 at 21:02
  • Your custom exception is wrong. All inherited exceptions must be `[Serializable]`. http://msdn.microsoft.com/en-us/library/ms182350.aspx, http://msdn.microsoft.com/en-us/library/ms182151.aspx – SLaks Jan 26 '11 at 21:05
  • well i have a unit test that i compare from expected and actual result this why i store it in a string and compare it with an expected value – jprbest Jan 26 '11 at 21:07
  • @jpr: Then you'll need to use `ex.ToString()` and not serialization. I don't think the serialized bytestreams will ever be identical. – SLaks Jan 26 '11 at 21:08
  • probably it is the best way, cause i was serializing all my objects so this why i try to the same – jprbest Jan 26 '11 at 21:10