0

I am looking for handling JS errors using try-catch statement, but its not alerting the Error the way I supposed it has to pop up error. Let me know what I am doing wrong here in the code.

Code testing browser - FireFox

Code -

<script type="text/javascript">
try {
document.write(parseInt("15");  // Broken code
}
catch(e) {
alert("Error " + e.description);
}
</script>

When I firebug the code its only showing the error in the try block that was all supposed to be but no alert pop up that ss what I am expecting.

Site reference I made before asking -

http://www.tutorialspoint.com/javascript/javascript_error_handling.htm

Error handling in javascript [Answer is referring to the same above mentioned URL]

Community
  • 1
  • 1
swapnesh
  • 26,318
  • 22
  • 94
  • 126
  • 2
    You can't catch syntax errors. – JJJ Apr 12 '13 at 05:57
  • @Juhana so is there any way in JS Like what we did in php ex- check mysql conn established or not ..if not throw error..something like that ? – swapnesh Apr 12 '13 at 05:58
  • See for example http://stackoverflow.com/questions/2526129/is-it-possible-to-programatically-catch-javascript-syntaxerrors – JJJ Apr 12 '13 at 05:58
  • Runtime errors that can be caught with try-catch. – roshan lal Apr 12 '13 at 06:02
  • @Juhana In the duplicate link you mentioned referring to use eval() is it really required even for this simple stuff..no other way?? – swapnesh Apr 12 '13 at 06:05
  • 1
    You can't catch syntax errors because the interpreter/compiler can't understand the code. It doesn't know where the `catch()` is and what to do with it. So yes, `eval()` is the only choice AFAIK. – JJJ Apr 12 '13 at 06:06
  • @Juhana Thx for the answer and link..I think its ok to stick with FIREBUG instead of controlling and alerting errors in JS way :) – swapnesh Apr 12 '13 at 06:10

0 Answers0