try and catch javascript unexpected identifier - Stack Overflow

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

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745406775a4626349.html

相关推荐

  • try and catch javascript unexpected identifier - Stack Overflow

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

    5小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信