捕获异常,然后抛出/发送异常并继续

时间:2012-09-23 18:18:13

标签: c# winforms exception try-catch

所以标题可能有点误导,但我想要完成的是读取一个文件数组,然后将它们组合成一个,这就是我现在所处的位置。

问题是我有一个查找异常“FileNotFoundException”的catch,当调用它时我想继续我的try语句(使用“continue”)但让用户知道该文件丢失。

我的设置是从表单调用的类(它在错误应该出现的形式中)

我考虑创建一个可以从我的表单中注册的事件,但这是正确的方法吗?

    public void MergeClientFiles(string directory)
    {
        // Find all clients
        Array clients = Enum.GetValues(typeof(Clients));

        // Create a new array of files
        string[] files = new string[clients.Length];

        // Combine the clients with the .txt extension
        for (int i = 0; i < clients.Length; i++)
            files[i] = clients.GetValue(i) + ".txt";

        // Merge the files into directory
        using (var output = File.Create(directory))
        {
            foreach (var file in files)
            {
                try
                {
                    using (var input = File.OpenRead(file))
                    {
                        input.CopyTo(output);
                    }
                }
                catch (FileNotFoundException)
                {
                    // Its here I want to send the error to the form
                    continue;
                }
            }
        }
    }

5 个答案:

答案 0 :(得分:3)

您希望该方法完成其工作并向用户报告问题,对吧? 然后Oded提出了正确的建议。通过小修改,代码可能如下所示:

    public List<string> MergeClientFiles( string path )
    {
        // Find all clients
        Array clients = Enum.GetValues( typeof( Clients ) );

        // Create a new array of files
        string[] files = new string[clients.Length];

        // Combine the clients with the .txt extension
        for( int i = 0; i < clients.Length; i++ )
            files[i] = clients.GetValue( i ) + ".txt";
        List<string> errors = new List<string>();

        // Merge the files into AllClientData
        using( var output = File.Create( path ) ) {
            foreach( var file in files ) {
                try {
                    using( var input = File.OpenRead( file ) ) {
                        input.CopyTo( output );
                    }
                }
                catch( FileNotFoundException ) {
                    errors.Add( file );
                }
            }
        }
        return errors;
    }

然后,在调用者中,您只需检查MergeClientFiles是否返回非空集合。

答案 1 :(得分:2)

您可以将异常收集到List<FileNotFoundException>中,并且在迭代结束时,如果列表为空,则抛出一个自定义异常,将此列表分配给相应的成员。

这将允许调用上述代码的任何代码捕获您的自定义异常,迭代FileNotFoundException并通知用户。

答案 2 :(得分:1)

您可以定义一个作为方法参数传递的委托。

public delegate void FileNotFoundCallback(string file);

public void MergeClientFiles(string directory, FileNotFoundCallback callback)
{
    // Find all clients
    Array clients = Enum.GetValues(typeof(Clients));

    // Create a new array of files
    string[] files = new string[clients.Length];

    // Combine the clients with the .txt extension
    for (int i = 0; i < clients.Length; i++)
        files[i] = clients.GetValue(i) + ".txt";

    // Merge the files into directory
    using (var output = File.Create(directory))
    {
        foreach (var file in files)
        {
            try
            {
                using (var input = File.OpenRead(file))
                {
                    input.CopyTo(output);
                }
            }
            catch (FileNotFoundException)
            {
                // Its here I want to send the error to the form
                callback( file );
                continue;
            }
        }
    }
}

答案 3 :(得分:0)

要获得一些灵感,请查看c#中新的并行结构的文档,例如Parallel.For和Reactive Framework(rx)。

首先,在AggregateException中收集异常,在Rx中,异常通过回调接口传递。

我认为我更喜欢Parallel.For中使用的方法,但选择最适合您情景的方法。

答案 4 :(得分:0)

您应该主动检查文件是否存在,然后不尝试打开文件,而不是捕获FileNotFoundException

您可以更改方法以返回合并文件列表,缺失文件列表或所有文件列表以及合并或缺失的指示符。返回单个列表可以让调用者一次性处理丢失的文件,并知道丢失了多少文件,而不是像事件或回调一样逐一处理。

相关问题