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 04 Answers
Reset to default 5It 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!