从Catch块返回表单 [英] return to form from Catch block

查看:97
本文介绍了从Catch块返回表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向以下方法添加一些错误处理,但我不确定如何在记录消息后返回到表单:



  private  ArrayList FileSearch( string  sDir, string  environment)
{

ArrayList configFiles = new ArrayList();
ArrayList directories = new ArrayList();
尝试
{
// 从提供的路径获取目录和子目录列表
directories.Add(sDir);
foreach string d in Directory.GetDirectories(sDir, *,System.IO.SearchOption.AllDirectories))
{
directories.Add(d);
}

// 获取目录列表中基于的文件列表搜索字符串
foreach string d 目录中)
{
foreach string f Directory.GetFiles(d,environment))
{
configFiles.Add(f);
}
}
}
catch (例外)
{
Log( 所选应用程序版本没有配置文件。);
// MessageBox.Show(所选应用程序版本没有文件。,DiffyPop );
// 返回null;
}

return configFiles;

}

解决方案

您的策略,在这种情况下,可能取决于您预计需要处理的错误类型,以及您可能需要评估执行此方法的结果。



我可以向您保证此代码.. 。在运行时 ...没有任何try / catch ...可能有一个错误来源:你使用无效的目录名称调用它。



您可以通过测试目录是否存在于方法的开头来处理:

 if(!Directory.Exists(sDir))
{
Log( System.IO.DirectoryNotFoundException+...目录不存在);
返回null;

}

或者,你可以变得花哨并在那里抛出错误......在'catch block中处理它。



一旦方法返回,你可以像这样测试它:

 ArrayList ConfigFiles = FileSearch(theFilePath,theFileSpec); 

if (!(ConfigFiles == null || ConfigFiles.Count = = 0 ))
{
// processFiles(ConfigFiles);
}

或者,您可以采取不同的操作,具体取决于是否存在错误(返回null)或零结果计数。 />


但是,我并不是想用try / catch块跟你说话:我只是想提高你对你的选择的认识! br />


假设在执行扫描目录列表并收集符合您规范的文件的代码时确实发生了一些错误(宇宙射线,黑客):在这种情况下你可能有一组有效的部分结果:如果你在错误上返回null,你将扔掉任何部分结果。如果你在错误时返回ArrayList,那么你可以使用任何部分结果。



选择你的。


< blockquote>返回null,如在注释掉的版本中,或者只返回一个空列表:

  catch  (例外)
{
Log( 所选应用程序版本没有配置文件。 );
return new ArrayList();
}





顺便说一句:你确实意识到Arraylist已经非常非常老了,还有很多更好的选择,例如通用列表< T>和其他收藏品?


I'm trying to add some error handling to the below method, but i'm not sure how I can return to the form after logging the message:

private ArrayList FileSearch(string sDir, string environment)
{

    ArrayList configFiles = new ArrayList();
    ArrayList directories = new ArrayList();
    try
    {
        // gets a list of directories and sub dirs from a provided path
        directories.Add(sDir);
        foreach (string d in Directory.GetDirectories(sDir, "*", System.IO.SearchOption.AllDirectories))
        {
            directories.Add(d);
        }

        //gets a list of files in the directory list based on the search string
        foreach (string d in directories)
        {
            foreach (string f in Directory.GetFiles(d, environment))
            {
                configFiles.Add(f);
            }
        }                
    }
    catch (Exception)
    {
        Log("There are no config files for selected App version.");
        //MessageBox.Show("There are no files for the selected App version.", "DiffyPop");
        //return null;
    }

    return configFiles;
    
}

解决方案

Your strategy, in a case like this, may depend on what type of errors you anticipate you need to handle, and your potential need to evaluate the results of executing this method.

I can assure you this code ... at run-time ... without any try/catch ... has potentially one source of errors: your calling it with an invalid Directory name.

You could handle that by testing whether the Directory exists at the start of the method:

if (! Directory.Exists(sDir))
{
     Log("System.IO.DirectoryNotFoundException " + "... Directory does not exist"); 
     return null;

}

Or, you could "get fancy" and throw an error there ... handling it in a 'catch block.

Once the method returns, you can test it like this:

ArrayList ConfigFiles = FileSearch(theFilePath, theFileSpec);

if (!(ConfigFiles == null || ConfigFiles.Count == 0))
{
    // processFiles(ConfigFiles);
}

Or, you could take different actions depending on whether there was an error (null returned) or a zero result count.

However, I'm not trying to "talk you out of" using a try/catch block: I'm just trying to contribute to your awareness of your choices !

Suppose some error (cosmic rays, hackers) did occur while executing the code that scans the list of Directories and "harvests" their files that match your specification: in that case you might have a valid set of partial results: if you return null on an error, you will "throw away" any partial results. If you return the ArrayList on an error, in that case, you could use any partial results.

The choice is yours.


Return null, as in your commented out version, or just return an empty list:

catch (Exception)
{
    Log("There are no config files for selected App version.");
    return new ArrayList();
}



BTW: You do realize that Arraylist is very, very old, and there are much better alternatives such as the generic List<T> and other collections?


这篇关于从Catch块返回表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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