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]

7
  • 2
    You can't catch syntax errors. Commented Apr 12, 2013 at 5: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 ? Commented Apr 12, 2013 at 5:58
  • See for example stackoverflow.com/questions/2526129/… Commented Apr 12, 2013 at 5:58
  • Runtime errors that can be caught with try-catch. Commented Apr 12, 2013 at 6:02
  • 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. Commented Apr 12, 2013 at 6:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.