30

I was wondering if there is a way to not specify the argument when doing a JS try/catch. Every time I try this though, the try/catch doesn't work.

The working version:

try{
  //Breaking code
} catch(e){
  //Nothing happens here
}

What I have in mind (No 'e'):

try{
  //Breaking code
} catch(){
  //Nothing happens here
}
Krisztián Balla
  • 17,664
  • 13
  • 63
  • 76
Dean Martin
  • 1,173
  • 3
  • 17
  • 26

5 Answers5

52

Optional catch binding in 2019

Node.js

In Node.js, this feature is called Optional Catch Binding and is supported since Node.js version 10.3, see https://node.green.

Typescript

In Typescript, this is allowed since version 2.5.

Browser support

  • Chrome: since 68
  • Firefox: since 58
  • Edge, IE, Safari: no support for now

Standard

The proposal is currently Stage 4, meaning that its implementation is finished and it is guaranteed to be included into the next version of the ECMAScript standard.

So this is a perfectly legitimate syntax now according to the standard if you are using Node.js or transpiling your browser code using Babel:

try {

} catch {
  // No need for the `(error)` after `catch`!
}
  • One important note is that in Typescript, if you target `esnext` this syntax will be *emitted*, breaking browsers like Edge that don't support it. (I believe there's actually a merged PR to add this capability to Edge, but it was last year and still hasn't landed. I can't find the page I read this on in my history now...) – Coderer Dec 04 '19 at 16:09
12

This is an outdated answer. It no longer applies to the current version of JavaScript. See other answers for details.


You just can't. The spec says that there must always be an identifier inside parens after catch.

izogfif
  • 3,976
  • 2
  • 31
  • 24
Jon
  • 413,451
  • 75
  • 717
  • 787
  • 3
    It should be noted that as of Firefox 58 and at least some recent Chrome versions, you can write `try {...} catch {...}` (that is, omit the parens and the identifier). However, I would advise against it, as it can lead to some hard-to-find parsing errors (works in one version of Chrome and Firefox, but not in another). See [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch). – Abel Apr 24 '18 at 12:40
3

The specification gives the grammar for a catch block:

Catch :

  catch ( Identifier ) Block

And goes on to state that:

When a catch clause catches an exception, its Identifier is bound to that exception

So it is a syntax error to omit the identifier from a catch block.

James Allardice
  • 160,885
  • 21
  • 326
  • 309
3

Simply omit the parenthese, like this:

try {
    // Code...
} catch {
    // Code...
}
Marius Tancredi
  • 971
  • 1
  • 9
  • 9
  • 1
    No, this is wrong and does not work in Internet Explorer. Please read the accepted answer. – Krisztián Balla Aug 20 '18 at 13:30
  • 9
    @JennyO'Reilly This is not wrong, it's part of ECMAScript 2019 specification: https://tc39.github.io/ecma262/#prod-Catch If you need to support IE, then use Babel. Many things won't work in IE, and it's IE's fault. – Marius Tancredi Aug 21 '18 at 03:18
  • 1
    Doesn't work in Node (8.11), either. Works in 10.10, but the former is flagged as "recommended for most users", so it can't be ignored. – riv Sep 09 '18 at 13:10
  • @riv Node 10 will become the recommended in October 2018, that's less than 2 months away, I wouldn't consider it a big problem. All major browsers beside Edge support this now, and Edge is working on it: https://github.com/Microsoft/ChakraCore/pull/5310 – Marius Tancredi Sep 10 '18 at 00:33
  • The PR is merged into ChakraCore, so Edge should be able to handle the new syntax in the update next spring (1903). Latest LTS of Node is 10 now, so this should be safe enough by the end of next year (2019). – Marius Tancredi Nov 28 '18 at 02:06
  • FWIW, I'm on Windows 18363, which I think maps to 1909, and Edge still fails with `Expected '('` when it finds a `catch { ... }` statement. – Coderer Dec 04 '19 at 13:16
  • 1
    @Coderer Unfortunately Microsoft has given up on the legacy Edge. The new Edge supports this syntax. – Marius Tancredi Dec 05 '19 at 17:50
1

Agreed, it's mandatory so that you can handle the error fully - even if you know what the error is likely to be. In truth, just prod in a variable name and don't use it within your catch routine :)

PeteAUK
  • 898
  • 6
  • 14