Roslyn-analyzers: CA1031 incorrectly fires on derived exceptions if no variable is assigned

Created on 17 Sep 2019  路  4Comments  路  Source: dotnet/roslyn-analyzers

Analyzer package

Microsoft.CodeAnalysis.FxCopAnalyzers

Package Version

2.9.5-beta1.final

Diagnostic ID

CA1031

Repro steps

try
{
    //something
}
catch (CustomException)
{

}

Expected behavior

No warning

Actual behavior

Warning CA1031 Modify 'MyFunction' to catch a more specific exception type, or rethrow the exception.

Workaround

try
{
    //something
}
catch (CustomException e)
{

}

Alternative

try
{
    //something
}
catch (CustomException)
{
    //something
    throw;
}
Resolution-Duplicate

Most helpful comment

@fschlaef I opened your project in latest VS2019 16.3 preview drop and I don't see this:

image

Additionally, your screenshot shows the CA1031 squiggle spans the entire catch block, which was explicitly fixed to only span the catch keyword prior to version 2.9.4 of the analyzer package with https://github.com/dotnet/roslyn-analyzers/pull/2519. So you are almost certainly on an older package. Have you installed https://marketplace.visualstudio.com/items?itemName=VisualStudioPlatformTeam.MicrosoftCodeAnalysis2019 by any chance?

All 4 comments

Duplicate of https://github.com/dotnet/roslyn-analyzers/issues/2518. Verified this does not repro on 2.9.4 or 2.9.5-beta1.final packages.

@mavasani I don't know how you verified this but this is trivial to reproduce.

CA1031Repro.zip

.Net Core 3, VS 2019 Preview 4

ca1031

@fschlaef I opened your project in latest VS2019 16.3 preview drop and I don't see this:

image

Additionally, your screenshot shows the CA1031 squiggle spans the entire catch block, which was explicitly fixed to only span the catch keyword prior to version 2.9.4 of the analyzer package with https://github.com/dotnet/roslyn-analyzers/pull/2519. So you are almost certainly on an older package. Have you installed https://marketplace.visualstudio.com/items?itemName=VisualStudioPlatformTeam.MicrosoftCodeAnalysis2019 by any chance?

@mavasani spot on ! I had indeed installed this extension. After removal, no more warning.

Thank you very much ! 馃憤

Was this page helpful?
0 / 5 - 0 ratings