Is there a way to catch multiple exceptions at once and without code duplication?

Technology CommunityCategory: .NET CoreIs there a way to catch multiple exceptions at once and without code duplication?
VietMX Staff asked 3 years ago
Problem

Consider:

try
{
    WebId = new Guid(queryString["web"]);
}
catch (FormatException)
{
    WebId = Guid.Empty;
}
catch (OverflowException)
{
    WebId = Guid.Empty;
}

Is there a way to catch both exceptions and only call the WebId = Guid.Empty call once?

Catch System.Exception and switch on the types:

catch (Exception ex)            
{                
    if (ex is FormatException || ex is OverflowException)
    {
        WebId = Guid.Empty;
        return;
    }

    throw;
}