marinasundstrom / CheckedExceptions

Enforce better exception handling in C#/.NET by ensuring exceptions are explicitly handled.
MIT License
4 stars 0 forks source link

Catch-clause silences exception of type in other catch clauses #19

Closed marinasundstrom closed 1 week ago

marinasundstrom commented 1 week ago

Related to #14 - Probably the same

In this case, where it was detected;

The catch clause catch (InvalidOperationException e) should propagate IOException (via Console.WriteLine) and not be affected by catch (IOException e) { } - not be silenced.

try
{
    Console.Write("Write number: ");

    var number = Console.ReadLine();

    if (number is null)
    {
        throw new InvalidOperationException("Argument is null");
    }

    var parsedNumber = Process(number);

    Console.WriteLine($"The number was: {parsedNumber}");
}
catch (IOException e) { }
catch (OutOfMemoryException e) { }
catch (ArgumentOutOfRangeException e) { }
catch (InvalidOperationException e)
{
    Console.WriteLine(e.Message);
}
marinasundstrom commented 1 week ago

Fixed in https://github.com/marinasundstrom/CheckedExceptionsAnalyzer/commit/17db96877e29f3091663521a1b87b5c6494e0693