dotnet / roslyn-analyzers

MIT License
1.57k stars 464 forks source link

Relax CA2219 in case of a rethrow #5869

Open Evangelink opened 2 years ago

Evangelink commented 2 years ago

Analyzer

Diagnostic ID: CA2219

Describe the improvement

I'd like to suggest to relax the rule to not report in case of a rethrow inside a try/catch within the finally clause.


public void M()
{
    try
    {
        M1();
    }
    finally
    {
        M2();
        try
        {
            MethodThatMightThrow();
        }
        catch (Exception ex)
        {
            // logging
            throw; // Do not raise here as we are not really throwing but rather let an exception flow.
        }
    }
}

### Describe suggestions on how to achieve the rule

Do not report if the exception being thrown in the finally clause is a rethrow.
Evangelink commented 2 years ago

@buyaa-n Any opinion?

gologames commented 1 month ago

Hello,

I want to up this topic. Agree that rethrow in finally might be a normal exception flow. For example:

//Open connection
try
{
    //Write data to connection
}
finally
{
    try
    {
        //Close connection
    }
    catch (Exception ex)
    {
        //Report detailed issue on closing connection
        throw; //Rethrow exception to cancel further execution flow
    }
}