为什么要引发异常并进行纠正 [英] Why exception is raised and correcting it

查看:86
本文介绍了为什么要引发异常并进行纠正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为History.xml的xml文件,还有相应的History.vb,它只包含两个属性,即TItleName和FileName。

返回的Obj是正确的。但是在调试社区2015时,在执行后面的行时,立即进入抛出输出中的异常,如下所示:

异常抛出:mscorlib.dll中的'System.IO.FileNotFoundException'



返回的obj包含在history.xml文件中找到的所有对象。



使用以下完整代码用于反序列化history.xml文件。

I have an xml File named as History.xml and also corresponding History.vb which contains only two properties namely TItleName and FileName.
The Obj returned is correct. But Community 2015 while debugging, when executing the line following Try immediately proceeds to Throw with an exception in output as follows:
Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll

The obj returned contains all objects that are found in history.xml file.

The following code in full is used for deserializing history.xml file.

Public Shared Function InitializeCollectionSource(Of T)(path As String) As ObservableCollection(Of T)
        Dim obj As ObservableCollection(Of T) = New ObservableCollection(Of T)
        If Not File.Exists(path) Then Return obj
        Using reader = New FileStream(path, FileMode.Open)
            Dim serializer = New XmlSerializer(obj.GetType)
            Try
                obj = CType(serializer.Deserialize(reader), ObservableCollection(Of T))
            Catch ex As Exception
                Throw
            Finally
                serializer = Nothing
            End Try
        End Using

        Return obj
    End Function



以下是我的问题:

1.请告知我为什么会抛出异常。

2。另外请建议如何纠正它。

谢谢。



评论后:

在适当尊重的情况下,反序列化的类具有无参数构造函数。此外,还实现了tostring,GetHashCode和Equals。我已经说过,初始化效果很好,即。对象反序列化完成。请检查你的评论。


The following is my questions:
1. Please inform me as to Why exception is thrown.
2. Also please suggest as to What is to be done to correct it.
Thanks.

After Comment:
With due respect, gentlemen, the class that is deserialized has parameterless constructor. Also, tostring, GetHashCode and Equals are implemented. I have stated already, that the initialization works well, ie. object deserialized is complete. Please review your comments.

推荐答案

假设问题出在序列化程序集上,否则知道它失败的原因应该是微不足道的....





如果我们告诉调试器停止所有异常,即使框架处理异常,它也会停止...



Google搜索XML序列化FileNotFoundException会提供很多链接。



https://www.google.ca/?client=safari&channel=ipad_bm#channel=ipad_bm& ; q = xml +序列化+ filenotfoundexception [ ^ ]



前两个结果:



http://stackoverflow.com/questions/8764742/filenotfoundexception-when-deserializing-xml [ ^ ]

http://stackoverflow.com/questions/1127431/xmlserializer-giving-filenotfoundexception-at-constructor [ ^ ]



因此这个问题是众所周知的,所以最好使用Google再次提出同样的问题...



否则不关心FileNotFoundException,那么您可以取消选中异常设置中的特定选项。



这将是nic e,如果有一种方法可以在特定情况下禁用这些异常,但它是打开或关闭...



NullReferenceException和TimeoutException是你会遇到的其他异常通常希望停止它们但不总是这样。



ASP.NET中的资源是另一种情况,如果你想在多个位置找到字符串,你有时必须处理异常按照定义的顺序,因为没有测试特定资源是否存在的功能(可能有一些方法可以做到但不像TrySomething函数那么简单......)



其他链接可能会告诉我如何修复它,但我从未尝试过。我只是在我自己的应用程序中忽略它,因为应用程序无论如何都按预期工作。
Assuming that the problem is with serialization assemblies as otherwise it should be trivial to know why it fails....


If we tell the debugger to stop on all exception, it will stop even if an exception in handled by the framework...

A Google search of XML serialization FileNotFoundException would give a lot of links.

https://www.google.ca/?client=safari&channel=ipad_bm#channel=ipad_bm&q=xml+serialization+filenotfoundexception[^]

First two results:

http://stackoverflow.com/questions/8764742/filenotfoundexception-when-deserializing-xml[^]
http://stackoverflow.com/questions/1127431/xmlserializer-giving-filenotfoundexception-at-constructor[^]

Thus this "problem" is well known so better to use Google that to ask same kind of question yet another time...

If you otherwise don't care for FileNotFoundException, then you can uncheck that specific option in you exception settings.

It would be nice if there would be a way to disable those exceptions in specific cases but it is either on or off switch...

NullReferenceException and TimeoutException are other exceptions that you would generally want to stop on them but not always.

Resources in ASP.NET is another case where you sometime have to handle exceptions if you want to find strings in multiple locations in a defined order as there a no function to test if a specific resource exists (well there might be some way to do it but not as easy as as TrySomething function...)

Other links will probably tell how to fix that but I never tried it. I simply ignore that in my own application as the application works as expected anyway.


这篇关于为什么要引发异常并进行纠正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆