
org.sonar.plugins.csharp.S2327.html Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonarlint-omnisharp-plugin Show documentation
Show all versions of sonarlint-omnisharp-plugin Show documentation
Code Analyzer based on Omnisharp
Why is this an issue?
When multiple, adjacent try
statements have duplicate catch
and/or finally
blocks, they should be merged to
consolidate the catch/finally
logic for cleaner, more readable code. Note that this applies even when there is intervening code outside
any try
block.
Noncompliant code example
try
{
DoTheFirstThing(a, b);
}
catch (InvalidOperationException ex)
{
HandleException(ex);
}
DoSomeOtherStuff();
try // Noncompliant; catch is identical to previous
{
DoTheSecondThing();
}
catch (InvalidOperationException ex)
{
HandleException(ex);
}
try // Compliant; catch handles exception differently
{
DoTheThirdThing(a);
}
catch (InvalidOperationException ex)
{
LogAndDie(ex);
}
Compliant solution
try
{
DoTheFirstThing(a, b);
DoSomeOtherStuff();
DoTheSecondThing();
}
catch (InvalidOperationException ex)
{
HandleException(ex);
}
try // Compliant; catch handles exception differently
{
DoTheThirdThing(a);
}
catch (InvalidOperationException ex)
{
LogAndDie(ex);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy