最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does this code raise: `Uncaught SyntaxError: Unexpected token catch` - Stack Overflow

programmeradmin4浏览0评论

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.

Share Improve this question edited Mar 28, 2013 at 1:07 claymation asked Mar 27, 2013 at 14:47 claymationclaymation 2,5154 gold badges27 silver badges37 bronze badges 5
  • 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
Add a ment  | 

1 Answer 1

Reset to default 5

After 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';
}
发布评论

评论列表(0)

  1. 暂无评论