Skip to content

Silent Error Swallowed by Empty Catch

Quick summary

You wrote catch (e) {} with no logging or handling — failures vanish silently and bugs appear far from their source.

Why this happens

  • Adding an empty catch just to make the linter quiet

  • Catching too broadly (catch (e) {}) instead of specific error types

  • Forgetting to log, rethrow, or set a fallback inside the catch

Minimal example

✗ Broken code
function parseConfig(raw) {
  try {
    return JSON.parse(raw);
  } catch (e) {} // silent — caller gets undefined
}
const cfg = parseConfig('bad');
console.log(cfg.port); // TypeError: Cannot read properties of undefined
✓ Fixed code
function parseConfig(raw) {
  try {
    return JSON.parse(raw);
  } catch (e) {
    console.error('Failed to parse config:', e.message);
    return {};
  }
}
const cfg = parseConfig('bad');
console.log(cfg.port); // undefined — safe default

The empty catch swallows the JSON error and returns undefined, so the caller crashes later. Logging the error and returning a safe default makes the failure visible and the downstream code safe.

How to diagnose

  • Search for empty catch blocks: /catch\s*\([^)]*\)\s*\{\s*\}/

  • Add a console.error in each catch and rerun to surface hidden failures

  • Check whether the surrounding code assumes the try always succeeded

How to fix

  • Log the error with enough context to diagnose it

  • Re-throw if you cannot handle the error at this level

  • Catch only the specific error type you can actually recover from

How to prevent

  • Enable a linter rule (no-empty) that forbids empty catch blocks

  • Always at least log the error in a catch, even during prototyping

Related Resources

Related Glossary

Related Lessons

  • Errors

    Learn about error handling.

← Back to language