C# XmlSerializer.Deserialize 方法可以抛出哪些类型的异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1326039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What types of Exceptions can the XmlSerializer.Deserialize method throw?
提问by George2
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?
对于这个方法,XmlSerializer.Deserialize
可能会抛出什么样的异常?XmlException
? InvalidOperationException
? 我没有从这个方法中找到任何异常描述信息。我的问题是这种方法可以抛出什么样的异常?
http://msdn.microsoft.com/en-us/library/dsh84875.aspx
http://msdn.microsoft.com/en-us/library/dsh84875.aspx
I am using VSTS2008 + C# + .Net.
我正在使用 VSTS2008 + C# + .Net。
thanks in advance, George
提前致谢,乔治
采纳答案by Nader Shirazie
Looks like primarily InvalidOperationException
.
看起来主要是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)
如果您阅读每个重载的文档,它将为您提供更多详细信息。例如,见XmlSerializer.Deserialize Method (XmlReader)
The InvalidOperationException
will contain more details about the specific error in its InnerException
property.
在InvalidOperationException
将包含有关其具体错误的更多细节InnerException
财产。
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.
在XmlSerializer.Deserialize Method (XmlSerializationReader)
可以抛出NotImplementedException
,但它是一个内部的API,并不意味着被你的代码中使用,所以不用担心。
Edit 2:
编辑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
.
所以看起来框架总是会抛出一个InvalidOperationException
.
Really, unless you're worried about mistakenly catching exceptions like ThreadAbortException
, you are probably safest catching allexceptions...
真的,除非您担心错误地捕获像 的异常,否则ThreadAbortException
捕获所有异常可能是最安全的......
Edit 3:
编辑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).
使用反射器:该Deserialize(stream)
方法使用 读取流XmlTextReader
并调用XmlSerializer.Deserialize Method (XmlReader, String)
. 该方法会引发InvalidOperationException
错误(根据文档)。
Edit 4:
编辑4:
Deserialize(stream)
can also throw a NullReferenceException
if stream
is null, because it calls the XmlTextReader(Stream)
constructor.
Deserialize(stream)
也可以抛出一个NullReferenceException
if stream
is null,因为它调用了XmlTextReader(Stream)
构造函数。
回答by Joon
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.
George,因为 .NET 中没有异常约定,最佳实践是捕获您可能想要对其进行特殊处理的任何特定异常,但也有一个捕获所有异常处理程序来适当地处理所有失败。
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.
我已经使用内置的 .NET 序列化实现了几个 XML 序列化解决方案,并且在所有情况下都只使用了一个 catch-all except 块,它遍历捕获异常的内部异常,将所有错误消息和异常类类型添加到一个字符串中信息。这样做总是提供足够的信息来调试任何序列化问题。
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.
在相关说明中,我通常做的是添加一个调试日志级别,将完整的 xml 记录到我的应用程序日志中,以便我可以检查它以尝试找出在调试序列化问题时出错的原因。
回答by Flávio Ivan
Exceptions from the XmlSerializer
Diagnosing the source of these problems can be tricky at first, because the exceptions from the XmlSerializer do not seem to provide a lot of information about the cause of their occurance; at least, they do not provide the information in a spot where developers typically would look.
In most cases, Serialize, Deserialize and even the XmlSerializer constructor throw a rather generic
System.InvalidOperationException
when an error occurs. This exception type can occur in many places in the .NET Framework; it is not specific to the XmlSerializer at all. To make matters worse, the exception's Message property only yields very generic information, as well.
XmlSerializer 的异常
一开始诊断这些问题的根源可能会很棘手,因为 XmlSerializer 的异常似乎没有提供大量有关其发生原因的信息;至少,他们不会在开发人员通常会看到的地方提供信息。
在大多数情况下,
System.InvalidOperationException
发生错误时,Serialize、Deserialize 甚至 XmlSerializer 构造函数都会抛出一个相当普遍的错误。这种异常类型可以在 .NET Framework 的很多地方发生;它根本不是特定于 XmlSerializer 的。更糟糕的是,异常的 Message 属性也只产生非常通用的信息。
This and other very handy tips about exceptions in the XmlSerializer can be found on the document Troubleshooting Common Problems with the XmlSerializer.
这个和其他关于 XmlSerializer 异常的非常方便的技巧可以在文档故障排除 XmlSerializer 的常见问题中找到。