等待任务时忽略特定类型的异常的更简单方法 [英] A simpler way of ignoring specific types of exceptions when awaiting a Task

查看:75
本文介绍了等待任务时忽略特定类型的异常的更简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在等待 Task code> 我想有一种简单的方法来忽略特定类型的异常,例如 OperationCanceledException TimeoutException 或其他任何类型.我想到了编写一个扩展方法的方法,该方法可以包装我的 Task ,并抑制我将其作为参数的 Exception 类型.所以我写了这个:

When awaiting a Task I would like to have an easy way to ignore specific types of exceptions, like OperationCanceledException or TimeoutException or whatever. I got the idea of writing an extension method that could wrap my Task, and suppress the Exception type I would give it as argument. So I wrote this one:

public static async Task Ignore<TException>(this Task task)
    where TException : Exception
{
    try
    {
        await task;
    }
    catch (Exception ex)
    {
        if (ex is TException) return;
        throw;
    }
}

我可以这样使用它,并且可以正常工作:

I can use it like this and it works OK:

await myTask.Ignore<OperationCanceledException>();

问题在于它仅支持一种类型的异常,我必须为两种类型编写另一种版本,另一种针对三种类型,另一种针对四种,依此类推.这已经失控了,因为我也想要此扩展程序的重载,该重载将忽略返回结果的任务的异常( Task< Result> ).因此,我还需要其他一系列扩展方法来解决这种情况.

The problem is that it supports only one type of exception, and I must write another version for two types, another one for three types, another one for four, and so on. This is getting out of hand, because I also want an overload of this extension that will ignore exceptions of tasks that return a result (Task<Result>). So I will need another series of extension methods to cover this case as well.

这是我的实现,用于在等待 Task< Result> 时忽略一种类型的异常:

Here is my implementation for ignoring one type of exception when awaiting a Task<Result>:

public static async Task<TResult> Ignore<TResult, TException>(
    this Task<TResult> task, TResult defaultValue)
    where TException : Exception
{
    try
    {
        return await task;
    }
    catch (Exception ex)
    {
        if (ex is TException) return defaultValue;
        throw;
    }
}

用法:

var result = await myInt32Task.Ignore<int, OperationCanceledException>(0);

我的问题是,我是否可以以一种可以处理多种类型的被忽略异常的方式编写这些方法,而不必为每种类型的数量编写单独的方法?

My question is, can I write these methods in a way that can handle multiple types of ignored exceptions, without having to write a separate method for each number of types?

推荐答案

可以,但是不能使用 Generics 来实现.

Yes you can, but you cannot do it using Generics.

如果您愿意将 Type s作为 params 传递,则可以执行以下操作:

If you're willing to pass Types as params, you can do this:

public static async Task<TResult> Ignore<TResult>
        (this Task<TResult> task, TResult defaultValue, params Type[] typesToIgnore)
        {
            try
            {
                return await task;
            }
            catch (Exception ex)
            {
                if (typesToIgnore.Any(type => type.IsAssignableFrom(ex.GetType())))
                {
                    return defaultValue;
                }

                throw;
            }
        }

现在,这吸引力不大了,而且您没有通用约束( where TException ...),但是应该完成工作.

Now, this is much less attractive and you don't have the generic constraint (where TException...) but it should get the job done.

这篇关于等待任务时忽略特定类型的异常的更简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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