Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:

C#
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;
    
}
Posted
Comments
Karthik_Mahalingam 17-Jan-14 4:16am    
what issue you are facing ??
pmcm 17-Jan-14 4:36am    
after no files are found in the directory, the code continues to process but I'd like to retun to the main form at this point with the message that there were no files found so the user can select a new version.
BillWoodruff 17-Jan-14 5:37am    
If you are calling this method from a Main Form, and you've handled any error, it should return to the Main Form. Are you calling it from your Main Form ?

See my response for further analysis.
pmcm 17-Jan-14 5:42am    
No it's not being called from the Main Form but the solution below allows me to catch the error without processing any extra unnecessary code once returned from this method:
ArrayList files = FileSearch(newPath, environment + "*");
if (files.Count == 0)
{
Log("No configs matching that environment prefix were found in the following path:" + Environment.NewLine + newPath);
return;
}
BillWoodruff 17-Jan-14 6:17am    
See if my response, below, addresses your concerns.

To deal with the issue of "return to the Main Form," you need to specify what the context ... the Container ... is in which your search method is being executed: a Form ? a Form shown modally ? What do you need to do ? Close the Form ?

Is it necessary to return the result of the search to some method on the Main Form ? Have you handled that ? Or, do you need to raise an Event that the Main Form has subscribed to, passing the search result in a Custom EventArgs instance ?

Need details.

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:
C#
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.
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 17-Jan-14 7:27am    
5
Return null, as in your commented out version, or just return an empty list:
C#
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?
 
Share this answer
 
Comments
pmcm 17-Jan-14 4:54am    
Yes I was aware of that, this application existed long before it became my responsibility. My aim at the minute is to make the message more user friendly before looking into updating the code that's there at present.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900