The try...catch statement allows you to test a block of code for errors. The try block contains the code to be run, and the catch block contains the code to be executed if an error occurs. Syntax:
| try { //Run some code here } catch(err) { //Handle errors here } |
Note that try...catch is written in lowercase letters. Using uppercase letters will generate a JavaScript error!
| try { adddlert("Welcome guest!"); } catch(err) { txt="There was an error on this page.\n\n"; txt+="Error description: " + err.description + "\n\n"; txt+="Click OK to continue.\n\n"; alert(txt); } |