no-useless-catch

禁止不必要的 catch 子句

配置文件 中的 "extends": "eslint:recommended" 属性启用了该规则

只重新抛出原始错误的 catch 子句是多余的,对程序的运行时行为没有影响。这些多余的子句可能会造成混乱和代码膨胀,因此最好禁止这些不必要的 catch 子句。

规则详情

此规则报告 catch 子句,只有 throw 捕获错误。

此规则的错误代码示例:

/*eslint no-useless-catch: "error"*/

try {
  doSomethingThatMightThrow();
} catch (e) {
  throw e;
}

try {
  doSomethingThatMightThrow();
} catch (e) {
  throw e;
} finally {
  cleanUp();
}

L9CfmI+BHDwYx59RS77HEHanEO40ClscOusGIf+RQNhowUc7Zz7f+Hb0VCfK93W9

/*eslint no-useless-catch: "error"*/

try {
  doSomethingThatMightThrow();
} catch (e) {
  doSomethingBeforeRethrow();
  throw e;
}

try {
  doSomethingThatMightThrow();
} catch (e) {
  handleError(e);
}

try {
  doSomethingThatMightThrow();
} finally {
  cleanUp();
}

何时不使用

0cGnaIg3gQrdLEQBgBUV3sKlssT3gvjqMng5IVC9dB+AUkRqN0cZ8UX8/o1xjIm7LvgVN5YgVEm2Y4Y4CAgg/rgQj21k8oZW+NWFfj8LILY6BlxeNm9RhW1EkmhNryUDY/qo9r5fBgPsSauuliBekA==