Skip to main content
ai-slop/csharp-broad-catchmanual fix

Broad catch.

A `catch (Exception)` that handles everything and never uses the caught exception. Problems you did not plan for disappear along with the one you did.

Flagged shape and clean shape.

Each example shows the exact code shape the rule is looking for, then the smallest version that keeps the intent without hiding risk or adding noise.

csharp-broad-catch.cs
✗ Flagged
public Invoice? LoadInvoice(string id)
{
    try
    {
        return _client.GetInvoice(id);
    }
    catch (Exception)
    {
        return null;
    }
}
Why this trips

Deterministic line match plus block parse: flags `catch (Exception ...)` or `catch (System.Exception ...)` whose body never references the caught variable.

csharp-broad-catch.cs
✓ Clean
public Invoice? LoadInvoice(string id)
{
    try
    {
        return _client.GetInvoice(id);
    }
    catch (InvoiceNotFoundException)
    {
        return null;
    }
}
What changed

Catch the specific exception types you can handle and let the rest propagate. At a deliberate boundary, log the caught exception or add a `when` filter so the failure stays diagnosable.

Agents catch the base type because it always compiles and the failing call might throw anything. The handler returns a default and the cause is gone.

How this rule is justified.

Rule id

ai-slop/csharp-broad-catch

Enforcing engine

ai-slop

Detector strategy

Deterministic line match plus block parse: flags `catch (Exception ...)` or `catch (System.Exception ...)` whose body never references the caught variable.

Legitimate code that is NOT flagged

Catches with a `when` filter, catches whose body uses the exception variable (to log or wrap it), empty `{ }` bodies, and `catch { throw; }` (covered by csharp-empty-catch-rethrow) are not flagged.

Evidence

Part of aislop's C# rule set. First-class C# support, including C#-specific AI-slop rules, arrived in aislop v0.14.1.