programming.protips.wiki
githubtwitter
/limit-what-you-catch
#error-handling

Limit what errors you catch in a try/catch block

Be specific when catching an error. Re-throw all other errors.

Bad:

try {
  result = divideNumbers(3, 0);
} catch (e) {
  console.error("You can't divide by zero!");
}

Good:

try {
  result = divideNumbers(3, 0);
} catch (e) {
  if (e instanceof DivideByZeroError) {
    console.error("You can't divide by zero!");
  } else {
    throw e;
  }
}

Why?

Lots of things could throw!

The message displayed to users or recovery logic inside the catch block may only apply to a certain type of error. But the catch block may be triggered with more errors types than you expect!

For example, someone could accidentally rename the divideNumbers function - and now we'd also be catching a ReferenceError - but still displaying the "you can't divide by zero!" error message.

If you have custom error handling or "recovery" logic, be specific what errors you want to handle, and re-throw all others.

Further reading