0

What would be the correct way to do the following:

try {
    card.focus();
} catch(ReferenceError e) {
    console.log(e)
} catch (e) {
    console.log('damn')
}

In python it would be:

try:
    card.focus()
except ReferenceError as e:
    print e
except:
    print 'damn'
  • Non-standard, but https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch. – StackSlave Oct 18 '18 at 22:57

5 Answers5

2

Use the keyword instanceOf in order to check the Error type.

try {
  card.focus();
} catch (error) {
  if (error instanceof ReferenceError) console.log("Not defined!");
}
Ele
  • 33,468
  • 7
  • 37
  • 75
  • thanks for this -- why are the `{...}` not required on the console.log statement? I've never seen a statement inside an `if` be written without brackets? –  Oct 18 '18 at 22:54
  • @DavidL `{...}` are optional if and only if there's exactly one statement to be controlled by the `if` conditional. – Ethan Herbertson Oct 18 '18 at 22:58
  • @DavidL The short answer is that javascript syntax is pretty laid back and you don't really need them here unless you want multiple statements. But even that isn't necessarily required thanks to the comma operator. – John Jones Oct 18 '18 at 22:58
  • 1
    @DavidL read this [Question](https://stackoverflow.com/questions/4797286/are-curly-braces-necessary-in-one-line-statements-in-javascript) – Ele Oct 18 '18 at 22:59
  • Noting that there are [issues with *instanceOf*](https://stackoverflow.com/a/35081067/257182), also see [*this answer*](https://stackoverflow.com/a/18057157/257182). – RobG Oct 19 '18 at 00:48
1

You can't do that, unfortunately. Closest thing is to do it in the catch block.

try {
  // something
} catch (e) {
  if (e.errorCode === 400) {
     // something
  } else if (e.errorCode === 404) {
     // something
  } ...
}
puddi
  • 811
  • 4
  • 10
  • what are the different error codes that correspond to "ReferenceError", etc.? Or is there a place to find those? –  Oct 18 '18 at 22:58
  • ah, no, this was to highlight the logic of doing it in the catch block. for exact error types you should use `typeOf`; the list of default javascript exceptions is https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types. – puddi Oct 18 '18 at 22:59
  • Important to note that you can also _create your own errors_, and those might have other properties you want to check. – puddi Oct 18 '18 at 23:00
1

The feature you're looking for is not part of any JavaScript standard, but was at one point available in certain browsers. It is not available in current browsers, however.

Instead, the docs suggest having a single catch block, within which you can test the type of the error and drive your desired behavior accordingly. So, something like:

try {
    card.focus();
} catch (e) {
    if (e instanceof ReferenceError) {
       // statements to handle ReferenceError exceptions
    } else {
       // statements to handle any unspecified exceptions
    }
}
0

If you have a class the catches error is an instance of you can use instanceof:

try {
   card.focus();
} catch(error) {
  if(error instanceof ReferenceError)console.log(error);
  else alert("Something went wrong! "  + error);
}
Sebastian Speitel
  • 7,166
  • 2
  • 19
  • 38
0

Using the instanceof operator is not recommended as it fails across frames and windows as the constructor.prototype in the origin context is different to the one in the testing context (and different objects are never == or ===).

There are other options.

You can get the error object's class, but that typically just returns "[object Error]":

Object.prototype.toString.call(new ReferenceError()); // [object Error]

However, you can also use the name property which is set to the name of the constructor, so:

new ReferenceError().name; // ReferenceError

is more reliable than instanceof. E.g.:

try {
  undefinedFunction();
} catch (e) {
  if (e.name == 'ReferenceError') {
    console.log(`Ooops, got a ${e.name}. :-)`);
  } else {
    console.log(`Ooops, other error: ${e.name}. :-(`);        
  }
}
RobG
  • 142,382
  • 31
  • 172
  • 209