Reliabilitywarnstrict preset

ai-guard/no-catch-without-use

Detects caught error variables that are declared but never inspected or logged

What it does

Flags `catch` clauses that declare an error parameter (e.g., `catch (err)`) but never reference that parameter in the catch body.

Why AI tools generate this pattern

If you catch an error and never use the bound variable, you're discarding the error information without even looking at it. This is subtly different from an empty catch (which is `error` level) — the body may have statements, but none of them involve the actual error. It suggests the error was caught accidentally or the handler was written without understanding what the error contains.

Code Examples

Incorrect (Flagged by AI Guard)
try {
  const data = JSON.parse(input);
  processData(data);
} catch (err) {
  // err is declared but never used
  res.status(400).json({ error: 'Invalid input' });
}
Correct (Safe & Deterministic)
try {
  const data = JSON.parse(input);
  processData(data);
} catch (err) {
  // Use err in the response or logging
  const message = err instanceof SyntaxError ? 'Invalid JSON format' : 'Processing failed';
  console.error('Parse error:', err);
  res.status(400).json({ error: message });
}

// Or use _ to explicitly signal you're ignoring it intentionally
try {
  await optionalCleanup();
} catch (_) {
  // intentionally ignored — cleanup is best-effort
}

How to Fix

  1. Either use the error variable (log it, include it in the response, wrap it) or rename it to `_` to signal the omission is intentional.

Nuances & False Positive Prevention

Legitimate usage can be exempted with inline disable comments (// ai-guard-disable-next-line no-catch-without-use) when appropriate.

Configuration

Enable or override this rule in your ESLint configuration:

eslint.config.mjs
// eslint.config.mjs
export default [
  {
    plugins: { 'ai-guard': aiGuard },
    rules: {
      'ai-guard/no-catch-without-use': 'warn',
    },
  },
];

Related Rules