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

try and catch javascript unexpected identifier - Stack Overflow

programmeradmin2浏览0评论

i'm tryin to identify the error i get in a javascrip function in my webpage, so i added

function guardarMisDatos() throws Exception {
try{
...
} catch (Exception e){
alert("error: ", e);
}

but when i open the page, the chrome web console gives me error at

function guardarMisDatos() throws Exception {

and the error type is "Uncaught syntaxerror: unexpected identifier" where is the error? is it a correct way to check way the function is not fired on the first click?

i'm tryin to identify the error i get in a javascrip function in my webpage, so i added

function guardarMisDatos() throws Exception {
try{
...
} catch (Exception e){
alert("error: ", e);
}

but when i open the page, the chrome web console gives me error at

function guardarMisDatos() throws Exception {

and the error type is "Uncaught syntaxerror: unexpected identifier" where is the error? is it a correct way to check way the function is not fired on the first click?

Share Improve this question asked May 14, 2012 at 14:36 StefanoStefano 454 silver badges14 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 5

It is JavaScript not Java. Lose the throws Exception!

Your code looks a lot like Java, not javaScript. The syntax for try/catch in javaScript goes like this:

try {
    // do stuff
} catch (e) {
    // something bad happened
}

Notice there is no throws and no type on e (since javascript is loosely typed)

Remove "throws Exception" and the catch reference to "Exception". To know what kind of exception it is, look at the e.name property, it'll be one of six things:

  • EvalError - An error in the eval() function has occurred.
  • RangeError - ut of range number value has occurred.
  • ReferenceError - An illegal reference has occurred.
  • SyntaxError - A syntax error within code inside the eval() function has occurred. All other syntax errors are not caught by try/catch/finally, and will trigger the default browser error message associated with the error. To catch actual syntax errors, you may use the onerror event.
  • TypeError - An error in the expected variable type has occurred.
  • URIError - An error when encoding or decoding the URI has occurred (ie: when calling encodeURI()).

These aren't constants, they're the actual string, as in if (e.name.toString()=="TypeError") There are a lot of other good things on the error object too, read more at http://www.javascriptkit./javatutors/trycatch2.shtml

Remove the throws Exception from your function definition. You do not need this in JavaScript. Besides that, why would your function ever throw an exception - you already catch it!

发布评论

评论列表(0)

  1. 暂无评论