The following JavaScript snippet raises SyntaxError: Unexpected token catch
immediately upon loading the script:
try {
// Client error (e.g., form validation)
if ((jqXHR.status === 400) && data.errors) {
// do something
}
// Server error (e.g., can't send email)
else if ((jqXHR.status === 500) && data.errors) {
// do something else
}
// Unknown error
else {
throw;
}
} catch(e) {
// Handle error
}
Unlike the other SyntaxError: Unexpected token
questions on SO, this problem is not caused by malformed JSON or simply forgetting a brace. There's something wrong with the syntax, but it's not immediately clear what it is.
The following JavaScript snippet raises SyntaxError: Unexpected token catch
immediately upon loading the script:
try {
// Client error (e.g., form validation)
if ((jqXHR.status === 400) && data.errors) {
// do something
}
// Server error (e.g., can't send email)
else if ((jqXHR.status === 500) && data.errors) {
// do something else
}
// Unknown error
else {
throw;
}
} catch(e) {
// Handle error
}
Unlike the other SyntaxError: Unexpected token
questions on SO, this problem is not caused by malformed JSON or simply forgetting a brace. There's something wrong with the syntax, but it's not immediately clear what it is.
- Your question is too localized and provides no research value. Most probably it will be closed. – VisioN Commented Mar 27, 2013 at 14:50
- 2 If you're a JavaScript expert, I suppose it provides no research value. But if you're banging your head against a desk trying to get a script to work, and google searches are revealing no answers, then I would think this question does have value. – claymation Commented Mar 27, 2013 at 14:52
- The same situation can happen in a GREAT number of other cases. How do you expect a user to find exactly yours question with nearly same title? The solution in the cases like that is to read manuals about exception handling and how to debug JavaScript code. – VisioN Commented Mar 27, 2013 at 14:58
-
1
Sure, there are many possible causes of SyntaxError, but very few that give this exact error. Forgetting that
throw
requires an expression is not such an unmon mistake, and is easy to overlook. Frequently, it's possible to find solutions to these kinds of errors with a quick Google search. More often than not, Google links to a SO question. I was trying to be helpful by providing an answer for a not-so-unmon and quite specific error. Instead of closing my question, why don't you go close the myriad other SyntaxError questions that clearly have to do with missing a parentheses or brace. – claymation Commented Mar 28, 2013 at 1:05 - I just earned the Famous Question badge for this question receiving 10,000 views. At least five people have found the answer helpful, so, maybe it does provide some research value, after all. – claymation Commented Dec 22, 2021 at 19:32
1 Answer
Reset to default 5After menting out each line in turn, I discovered the problem is with the line:
else {
throw;
}
I intended to throw a generic exception, but throw
requires an expression. Rewriting it like this fixes it:
else {
throw 'Unknown error';
}