Skip to content

Not effective - CA2200: Rethrow to preserve stack details #6284

@LiangZugeng

Description

@LiangZugeng

Analyzer

Diagnostic ID: CA2200: Rethrow to preserve stack details

Version: [SDK 7.0.100]

Describe the bug

dotnet build or the build action in Visual Studio for Mac didn't show the warning of CA2200.

Steps To Reproduce

  1. With .NET 7 SDK installed, create a new console app using dotnet new console -o TestConsoleApp
  2. Replace all content with the following code in Program.cs.
class TestsRethrow
{
    static void Main()
    {
        TestsRethrow testRethrow = new TestsRethrow();
        testRethrow.CatchException();
    }

    void CatchException()
    {
        try
        {
            CatchAndRethrowExplicitly();
        }
        catch (ArithmeticException e)
        {
            Console.WriteLine("Explicitly specified:{0}{1}",
               Environment.NewLine, e.StackTrace);
        }

        try
        {
            CatchAndRethrowImplicitly();
        }
        catch (ArithmeticException e)
        {
            Console.WriteLine("{0}Implicitly specified:{0}{1}",
               Environment.NewLine, e.StackTrace);
        }
    }

    void CatchAndRethrowExplicitly()
    {
        try
        {
            ThrowException();
        }
        catch (ArithmeticException e)
        {
            // Violates the rule.
            throw e;
        }
    }

    void CatchAndRethrowImplicitly()
    {
        try
        {
            ThrowException();
        }
        catch (ArithmeticException)
        {
            // Satisfies the rule.
            throw;
        }
    }

    void ThrowException()
    {
        throw new ArithmeticException("illegal expression");
    }
}

Content of the .csproj file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

Expected behavior

Warning should be reported that CA2200 was found.

Actual behavior

No warnings reported.

MSBuild version 17.4.0+18d5aef85 for .NET
  Determining projects to restore...
  Restored /Users/james/Projects/Temp/TestConsoleApp/TestConsoleApp.csproj (in 39 ms).
  TestConsoleApp -> /Users/james/Projects/Temp/TestConsoleApp/bin/Debug/net7.0/TestConsoleApp.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.64

Additional context

.NET SDK: 7.0.100
OS: MacOS Ventura

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThe product is not behaving according to its current intended design

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions