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'
1

5 Answers 5

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!");
}

Sign up to request clarification or add additional context in comments.

5 Comments

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?
@DavidL {...} are optional if and only if there's exactly one statement to be controlled by the if conditional.
@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.
@DavidL read this Question
Noting that there are issues with instanceOf, also see this answer.
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
  } ...
}

3 Comments

what are the different error codes that correspond to "ReferenceError", etc.? Or is there a place to find those?
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 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/….
Important to note that you can also create your own errors, and those might have other properties you want to check.
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
    }
}

Comments

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);
}

Comments

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}. :-(`);        
  }
}

Comments