Skip to content

Catch block with 'when' clause not correctly interpreted if or keyword is inserted #8

@NoTuxNoBux

Description

@NoTuxNoBux

Type narrowing with the when catch syntax only seems to be picked up correctly if the clause is relatively simple, for example:

public class Foo
{
    public async Task BarAsync()
    {
        HttpClient httpClient = new();
        HttpRequestMessage message = new();

        try
        {
            await httpClient.SendAsync(message);
        }
        catch (Exception e) when (e is HttpRequestException)
        {
            // Do something.
        }
    }
}

This correctly detects that HttpRequestException from SendAsync is handled. However:

public class Foo
{
    public async Task BarAsync()
    {
        HttpClient httpClient = new();
        HttpRequestMessage message = new();

        try
        {
            await httpClient.SendAsync(message);
        }
        catch (Exception e) when (e is HttpRequestException or JsonException)
        {
            // Do something.
        }
    }
}

This reports 'BarAsync()' may throw undocumented exception: HttpRequestException. Perhaps not all the types in the or block are scanned?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions