]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-ex-assign.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-ex-assign.md
1 ---
2 title: no-ex-assign
3 rule_type: problem
4 further_reading:
5 - https://bocoup.com/blog/the-catch-with-try-catch
6 ---
7
8
9
10 If a `catch` clause in a `try` statement accidentally (or purposely) assigns another value to the exception parameter, it is impossible to refer to the error from that point on.
11 Since there is no `arguments` object to offer alternative access to this data, assignment of the parameter is absolutely destructive.
12
13 ## Rule Details
14
15 This rule disallows reassigning exceptions in `catch` clauses.
16
17 Examples of **incorrect** code for this rule:
18
19 ::: incorrect
20
21 ```js
22 /*eslint no-ex-assign: "error"*/
23
24 try {
25 // code
26 } catch (e) {
27 e = 10;
28 }
29 ```
30
31 :::
32
33 Examples of **correct** code for this rule:
34
35 ::: correct
36
37 ```js
38 /*eslint no-ex-assign: "error"*/
39
40 try {
41 // code
42 } catch (e) {
43 var foo = 10;
44 }
45 ```
46
47 :::