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

4
所以标题可能有点误导,但我的目标是读取一组文件并将它们合并成一个文件,这就是我现在的问题所在。
问题是我有一个catch语句,查找异常“FileNotFoundException”,当调用它时,我想继续我的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个回答

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是否返回非空集合即可。

我认为我会选择这个答案。让方法负责返回错误信息并让表单确定是否有任何错误需要报告是有道理的。 - Dumpen

2
您可以将异常收集到一个 List<FileNotFoundException> 中,在迭代结束时,如果列表不为空,则抛出自定义异常,并将该列表分配给相应的成员。
这将允许调用上述代码的任何代码捕获您的自定义异常,迭代处理 FileNotFoundException 并通知用户。

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;
            }
        }
    }
}

0

为了获得一些灵感,可以查看C#中新的并行构造的文档,例如Parallel.For和Reactive Framework(rx)。

在第一个例子中,异常被收集在AggregateException中,在Rx中,异常通过回调接口进行通信。

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


0

与其捕获FileNotFoundException,您应该积极检查文件是否存在,如果不存在,则不要尝试打开它。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接