]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-useless-catch.md
first commit
[pve-eslint.git] / eslint / docs / rules / no-useless-catch.md
1 # Disallow unnecessary catch clauses (no-useless-catch)
2
3 A `catch` clause that only rethrows the original error is redundant, and has no effect on the runtime behavior of the program. These redundant clauses can be a source of confusion and code bloat, so it's better to disallow these unnecessary `catch` clauses.
4
5 ## Rule Details
6
7 This rule reports `catch` clauses that only `throw` the caught error.
8
9 Examples of **incorrect** code for this rule:
10
11 ```js
12 /*eslint no-useless-catch: "error"*/
13
14 try {
15 doSomethingThatMightThrow();
16 } catch (e) {
17 throw e;
18 }
19
20 try {
21 doSomethingThatMightThrow();
22 } catch (e) {
23 throw e;
24 } finally {
25 cleanUp();
26 }
27 ```
28
29 Examples of **correct** code for this rule:
30
31 ```js
32 /*eslint no-useless-catch: "error"*/
33
34 try {
35 doSomethingThatMightThrow();
36 } catch (e) {
37 doSomethingBeforeRethrow();
38 throw e;
39 }
40
41 try {
42 doSomethingThatMightThrow();
43 } catch (e) {
44 handleError(e);
45 }
46
47 try {
48 doSomethingThatMightThrow();
49 } finally {
50 cleanUp();
51 }
52 ```
53
54 ## When Not To Use It
55
56 If you don't want to be notified about unnecessary catch clauses, you can safely disable this rule.