views:

735

answers:

2

Hello everyone,

For this method, XmlSerializer.Deserialize, what kinds of exception may be thrown? XmlException? InvalidOperationException? I did not find any exception description information from this method. My question is what kinds of exception could be thrown from this method?

http://msdn.microsoft.com/en-us/library/dsh84875.aspx

I am using VSTS2008 + C# + .Net.

thanks in advance, George

+3  A: 

Looks like primarily InvalidOperationException.

If you go through the documentation for each of the overloads, it will give you more details. For example, see XmlSerializer.Deserialize Method (XmlReader)

The InvalidOperationException will contain more details about the specific error in its InnerException property.

Edit:

The XmlSerializer.Deserialize Method (XmlSerializationReader) can throw a NotImplementedException, but it is an internal API and is not meant to be used by your code, so don't worry about it.

Edit 2:

This code:

var ms = new System.IO.MemoryStream();
var deser = new System.Xml.Serialization.XmlSerializer(typeof(string));
deser.Deserialize(ms);

throws:

System.InvalidOperationException: There is an error in XML document (0, 0). ---
System.Xml.XmlException: Root element is missing.
  at System.Xml.XmlTextReaderImpl.Throw(Exception e)
... <snip> ...

So it really looks like the framework will always throw an InvalidOperationException.

Really, unless you're worried about mistakenly catching exceptions like ThreadAbortException, you are probably safest catching all exceptions...

Edit 3:

Using Reflector: The Deserialize(stream) method reads the stream using an XmlTextReader and calls the XmlSerializer.Deserialize Method (XmlReader, String). That method throws an InvalidOperationException on error (according to the docs).

Edit 4:

Deserialize(stream) can also throw a NullReferenceException if stream is null, because it calls the XmlTextReader(Stream) constructor.

Nader Shirazie
But for this overload, there is no exception thrown. http://msdn.microsoft.com/en-us/library/dsh84875.aspxSuppose we input a MemoryStream into this overload, and what kinds of exception may be thrown?
George2
Thanks for your update, my major concern is about the overload which accept Stream (e.g. when we supply a MemoryStream), in MSDN I cannot find which kinds of exception may be thrown? Any ideas?The overload I mentioned is http://msdn.microsoft.com/en-us/library/dsh84875.aspx
George2
Thanks nader, I like your edit2 section, but where do you find the XmlException and InvalidOperationException? I did not find in MSDN, but how do you find it? :-)So magic of you.
George2
@George2: That is just the stack trace from the exception (since an empty memory stream is not valid for deserialization) from my test code. The XmlException will be the "InnerException" property on the "InvalidOperationException", which is what the Deserialize method will throw. None of the methods are documented to throw XmlException -- they are all documented to throw InvalidOperationException.
Nader Shirazie
@George2: Have a look at my last update. The overload you're looking at will throw an InvalidOperationException, since it doesn't do anything except call Deserialize(XmlReader,String)
Nader Shirazie
Thanks, so I only need to catch NullReferenceException and InvalidOperationException?
George2
Any new updates for my comments? :-)
George2
@George2: As far as I can tell, yes those are the two types of exceptions that are thrown.
Nader Shirazie
Thanks nader guru, my question is answered. :-)
George2
haha, no problem man.
Nader Shirazie
+1  A: 

George, because there is no exception contract in .NET, the best practice is to catch any specific exceptions that you may want to do special processing for, but to also have a catch-all exception handler that handles all failures appropriately.

I have implemented several XML serialization solutions using the built-in .NET serialization, and have in all cases just used a catch-all except block, which walks the innerexceptions of the caught exception, adding all error messages and exception class types to a string message. Doing it like that has always provided enough information to debug any serialization issues.

On a related note, what I normally do is to add a debug log level which logs the full xml to my application's log, so that I can inspect it to try and figure out what when wrong when debugging a serilization issue.

Joon
Thanks Joon, my idea is the same as yours, catch expected exception only and log for all exception information. But my question is, for this specific overload deserialization method http://msdn.microsoft.com/en-us/library/dsh84875.aspx what kinds of exception may be thrown if deserialization fails?
George2
Any new updates for my comments? :-)
George2
@George: it doesn't matter. Whatever the answer is today, it may change tomorrow. You need to act ask though you don't know what exceptions may be thrown because, you don't know.
John Saunders