C#

C#

Made by DeepSource

Variable declared when catching Exception is unused CS-R1084

Anti-pattern
Major
Autofix

Exception variable declared in catch should be used. If you do not wish to use the trapped Exception's details, consider omitting the variable from the catch declaration.

Bad Practice

try
{
}
catch (ArgumentException e)
{
    // `e` is unused
    Console.WriteLine("Error description");
}

Recommended

// Alternative 1: Use the trapped Exception
try
{
}
catch (ArgumentException e)
{
    Console.WriteLine(e);
}

// Alternative 2: Remove exception variable from catch declaration
try
{
}
catch (ArgumentException)
{
    Console.WriteLine("Error description");
}

Reference