40

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
}
3
  • What are you trying to achieve? Commented Feb 7, 2014 at 9:56
  • Why do you want that? If you won't handle the exception then just leave the catch body block empty. Commented Feb 7, 2014 at 9:56
  • 4
    Since 2019, the (e) is optional. stackoverflow.com/a/56001361/6269864 Commented Aug 13, 2019 at 6:24

6 Answers 6

71

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`!
}
Sign up to request clarification or add additional context in comments.

1 Comment

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...)
13

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.

1 Comment

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.
5

Simply omit the parenthese, like this:

try {
    // Code...
} catch {
    // Code...
}

8 Comments

No, this is wrong and does not work in Internet Explorer. Please read the accepted answer.
@JennyO'Reilly This is not wrong, it's part of ECMAScript 2019 specification: 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.
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 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: github.com/Microsoft/ChakraCore/pull/5310
@Coderer Unfortunately Microsoft has given up on the legacy Edge. The new Edge supports this syntax.
|
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.

Comments

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 :)

Comments

1

MDN provides a solid content about the try-block:

When an exception is thrown in the try-block, exception_var (i.e., the e in catch (e)) holds the exception value. You can use this identifier to get information about the exception that was thrown. This identifier is only available in the catch-block's scope. If you don't need the exception value, it could be omitted.

function isValidJSON(text) {
  try {
    JSON.parse(text);
    return true;
  } catch {
    return false;
  }
}

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.