2

OracleException has no public constructors nor any way to get a new instance. I tried my XmlSerializerHelper class, but it requires a public parameterless constructor.

I used BinaryFormatter to serialize the OracleException and wrote it to a file.

How can I serialize OracleException in a file, and deserialize too using XmlSerializer -for testing reasons-?.

Reference: http://geekswithblogs.net/WillSmith/archive/2008/07/25/testing-oracleexception.aspx

PD: Is better SoapFormatter or BinaryFormatter ?

Code

SerializationHelper.Serialize(@"C:\Temp\ExcepcionOracle.bin", ex);

var exOra = SerializationHelper.Deserialize(@"C:\Temp\ExcepcionOracle.bin");


public static void Serialize(string fileName, Object obj)
        {
            var binaryFormatter = new BinaryFormatter();
            var fileStream = new FileStream(fileName, FileMode.Create);
            try
            {
                binaryFormatter.Serialize(fileStream, obj);
            }
            catch (SerializationException ex)
            {
                throw new ApplicationException("The object graph could not be serialized", ex);
            }
            finally
            {
                fileStream.Close();
            }
        }


        public static object Deserialize(string fileName)
        {
            var binaryFormatter = new BinaryFormatter();
            var fileStream = new FileStream(fileName, FileMode.Open);
            try
            {
                fileStream.Seek(0, SeekOrigin.Begin);
                return binaryFormatter.Deserialize(fileStream);
            }
            catch (SerializationException ex)
            {
                throw new ApplicationException("Serialization Exception: " + ex.Message);
            }
            finally
            {
                fileStream.Close();
            }
            return null;
        }  
2
  • Why do you need to serialize and deserialize an exception? Commented Nov 4, 2011 at 12:40
  • Re BinaryFormatter vs SoapFormatter... well, I would argue strongly "neither", but SoapFormatter is officially to be avoided (MSDN notes it is obsolete). BinaryFormatter is still hanging on by fingernails... The only time I use BinaryFormatter is to poke fun at it. Commented Nov 4, 2011 at 13:19

1 Answer 1

0

things like Exception simply aren't very suitable for xml serializers (and XmlSerializer in particular). In addition to the constructor issues (which some serializers can work around, and some can't), you are also likely to get issues with unexpected subclasses and arbitrary data in the collection.

If you are serializing as xml, you should probably just capture the key information you need - maybe the .Message and a few other things. Note also that in a client/server application the client doesn't really need to know much of the particulars of the failure - that should remain at the server. Either it is an exected error (invalid parameters, login issues, quota restrictions, etc), or it is an unexpected error. In the latter case: just say an unexpected error happened. The details would only be useful to a developer, and a developer should already have access to the server's error log.

Sign up to request clarification or add additional context in comments.

4 Comments

Agreed. Still, aren't all exceptions supposed to be marked [Serializable] and provide default constructors (for completely different reasons, e.g. remoting)? FxCop keeps nagging me about that... Are Oracle not following "best practices" here?
@Anders that is only useful to BinaryFormatter; the OP is asking about XmlSerializer
I know. I was just pointing out that this whole problem is because Oracle apparently hasn't followed the design guidelines which say that exceptions should have a public no-arg constructor. That said, I do agree that exceptions should not be passed to a client in this scenario.
@Anders it depends - for all I know, maybe this is an abstract base-class for a few different private exception types... horses for courses

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.