Skip to content

A catch that only rethrows; logging an error and rethrowing it ​

Code longer than it needs to be: one of the ways AI coding agents bloat software, from lessloc's pattern catalog. lessloc catches it as simpler.

What makes a catch that only rethrows a bad practice? ​

The standards that call it a bad practice: ESLint no-useless-catch; Sonar S2737; ruff TRY203; McCune, "Exception-Handling Antipatterns": Log and Throw.

What does lessloc do about a catch that only rethrows? ​

lessloc catches it as simpler.

  • simpler (tier 2): Brute-force code with a shorter idiom: == True, an if/else that returns the condition, an index loop that only reads xs[i], .filter(...).length > 0, a catch that only rethrows, a flag loop that is any(), a hand-built counter, an __init__ that @dataclass writes, a React Effect that only sets state. ruff and clippy add more.

Examples ​

Caught as simpler:

js
export async function load(f) {
  try {
    return await f();
  } catch (e) {
    throw e;
  }
}

Written well:

js
export async function load(f) {
  return f();
}

Caught as simpler:

js
export async function load(f) {
  try {
    return await f();
  } catch (err) {
    console.error("load failed", err);
    throw err;
  }
}

Written well:

js
export async function load(f) {
  try {
    return await f();
  } catch (err) {
    throw new Error("load failed", { cause: err });
  }
}

The other patterns in Code longer than it needs to be:

Released under the Apache-2.0 license. · llms.txt