我的错误观察器​​方法调用抛出异常。有什么帮助吗? [英] My error watcher method called throws an exception. Any help please?

查看:45
本文介绍了我的错误观察器​​方法调用抛出异常。有什么帮助吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们好,



我正在尝试检索并将目录元素从我的xml文件传递给错误观察者作为带过滤器的字符串。但后来我将找到的路径按名称传递给错误观察者作为字符串。我得到一个例外。代码直接移动到catch块。任何帮助将不胜感激。



提前感谢。



我尝试过:



Hello friends,

I am trying to retrieve and pass only directory elements from my xml file to the error watcher as string with a filter. But then i pass the found path by name into the error watcher as string. I get an exception. The code directly moves onto the catch block. Any help would be appreciated.

Thank in advance.

What I have tried:

public string[] Create(string settingsFile)
            {
                string FilePath = settingsFile;
                settingsFile = "my path/directory of the xml file";
                string[] a = new string[20];

                XmlTextReader xmlReader = new XmlTextReader(settingsFile);
                try
                {
                    while (xmlReader.Read())
                    {
                        switch (xmlReader.Name)
                        {
                            case "InputDataPath":
                                a[0] = xmlReader.ReadString();
                                ErrorWatcher(a[0], "Status.error");
                                continue;
                            case "BackupDataPath":
                                a[1] = xmlReader.ReadString();
                                ErrorWatcher(a[1], "Status.error");
                                continue;
                            case "ExportDatapath":
                                a[2] = xmlReader.ReadString();
                                ErrorWatcher(a[2], "Status.error");
                                continue;
                        }

                    }

                }
                catch (Exception e)
                {
                    return null;
                }
                return a;
                
            }

推荐答案

首先使用调试器:在函数的第一行放置一个断点,并通过调试器运行您的代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行它并仔细观察)以找出原因。
首先查看你通过的路径在:如果它是一个文件夹,那将无法工作。

如果看起来没问题 - 并且文件存在 - 继续执行直到错误发生。然后查看异常细节以找出它除外的原因 - 那里通常有一些非常有用的信息!可能,文件很糟糕,XML阅读器在你的try块之外抛出一个异常。



对不起,但是我们不能为你做到 - 给你时间学习一种新的(非常非常有用的)技能:调试!



自己做几个好处:记录异常,不要只是忽略它们并返回null - 当你这样做时,你不知道错误是什么,所以很难修复。

完成它后关闭你的XML阅读器(使用块是最简单的方法) - 如果你不这样做,它会一直打开,直到垃圾收集器为你或你的应用程序退出关闭它,以先到者为准。
Start by using the debugger: Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
Start by looking at the path you pass in: if it's a folder, that won't work.
If that looks ok - and the file exists - step on through until the error happens. Then look at the exception detail to find out why it excepted - there is usually some very helpful info in there! Probably, the file is bad and the XML reader is throwing an exception outside your try block.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

And do yourself a couple of favours: Log exception, don't just ignore them and return null - when you do that, you have no idea what the error was, so it's hard to fix.
And close your XML reader when you are finished with it (a using block is the simplest way) - if you don't it remains open until the garbage collector get around to closing it for you or your app exits, whichever comes first.


这篇关于我的错误观察器​​方法调用抛出异常。有什么帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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