如何从 try-catch 中简化 return 的语句

How to simplify return's statement from try-catch

如何简化以下代码:

try
{
    var metadata = GetMetadata();
    return metadata ?? _provider.GetLatestMetadata(guid);
}
catch (AuthenticationException)
{
    return _provider.GetLatestMetadata(guid);
}
catch (HttpUnauthorizedRequestException)
{
    return _provider.GetLatestMetadata(guid);
}
catch (WebException)
{
    return _provider.GetLatestMetadata(guid);
}
catch (VcenterException)
{
    return _provider.GetLatestMetadata(guid);
}

我想避免代码重复。

可能吗?

如果不想包罗万象,确实需要避免重复代码,可以使用异常过滤器捕获特定的异常:

try
{
    var metadata = GetMetadata();
    return metadata ?? _provider.GetLatestMetadata(guid);
}
catch (Exception ex) when ( ex is AuthenticationException
                            || ex is HttpUnauthorizedRequestException
                            || ex is WebException
                            || ex is VcenterException
                          )
{
    return _provider.GetLatestMetadata(guid);
}