Skip to main content

js/noCatchAssign

disallow reassigning exceptions in catch clauses

ESLint Equivalent: no-ex-assign

Examples

Invalid

try { } catch (e) { e; e = 10; }

 file.ts:1:23 lint/js/noCatchAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not reassign catch parameters.

    try { } catch (e) { e; e = 10; }
                           ^

  Use a local variable instead.


try { } catch (ex) { console.log('test'); ex = 10; }

 file.ts:1:42 lint/js/noCatchAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not reassign catch parameters.

    try { } catch (ex) { console.log('test'); ex = 10; }
                                              ^^

  Use a local variable instead.


try { } catch (ex) { [ex, test] = []; }

 file.ts:1:22 lint/js/noCatchAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not reassign catch parameters.

    try { } catch (ex) { [ex, test] = []; }
                          ^^

  Use a local variable instead.


try { } catch ({message, name}) { message = 'test'; name = 10; }

 file.ts:1:34 lint/js/noCatchAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not reassign catch parameters.

    try { } catch ({message, name}) { message = 'test'; name = 10; }
                                      ^^^^^^^

  Use a local variable instead.

 file.ts:1:52 lint/js/noCatchAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not reassign catch parameters.

    try { } catch ({message, name}) { message = 'test'; name = 10; }
                                                        ^^^^

  Use a local variable instead.


try { } catch (ex) { ({x: ex = 0} = {}); }

 file.ts:1:26 lint/js/noCatchAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not reassign catch parameters.

    try { } catch (ex) { ({x: ex = 0} = {}); }
                              ^^

  Use a local variable instead.


try { } catch (ex) { let a; ({x: a = ex = 0} = {}); }

 file.ts:1:37 lint/js/noCatchAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not reassign catch parameters.

    try { } catch (ex) { let a; ({x: a = ex = 0} = {}); }
                                         ^^

  Use a local variable instead.

Valid

try { } catch (e) { three = 2 + 1; }
try { } catch ({e}) { this.something = 2; }
function foo() { try { } catch (e) { return false; } }