Skip to content

Option to use [Throws(typeof(Exception))] as a catch the rest #301

@marinasundstrom

Description

@marinasundstrom

Add an option (and setting) to allow [Throws(typeof(Exception))] as a catch the of the exceptions.

This would be useful in this case:

    [Fact]
    [Throws(
        typeof(TrueException), typeof(NotNullException), typeof(EqualException), typeof(NotEmptyException), 
        typeof(Exception))]
    public void Excample()
    {
        var str = "42";
        var x = int.Parse(str);

        Assert.True(true);
        Assert.NotNull("foo");
        Assert.NotEmpty([1]);
        Assert.Equal("Hello world", str);
    }

Implementation details

This settings would turn of the diagnostic:

Avoid declaring exception type 'System.Exception'; use a more specific exception type (THROW003)

And not flag any other declared exceptions with:

Exception already handled by declaration of super type ('Exception') (THROW008)

When exception type Exception is declared. Effectively removing the type the inheritance hierarchy in this particular place.

Setting

Add to CheckedExceptions.settings.json:

{
    "treatThrowsExceptionAsCatchRest": true
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions