You can also use the try {} catch () {} statement block in Js. Example:
1. If a JS error occurs, ask if you want to continue checking.
Code:
- <SCRIPT type = "text/JavaScript">
- Function Test (){
- Try {
- Allet (); // alert is wrong
- } Catch (ERR ){
- Document. writeln ("catch exceptions and start to execute Catch Block statements ---> ");
- Document. writeln ("error name:" + err. Name + "---> ");
- Document. writeln ("error message:" + err. Message + "---> ");
- TXT = "Page error! "
- TXT + = "click" OK "to continue viewing the current page ,"
- TXT + = "click Cancel to return to the previous page. "
- If (! Confirm (txt )){
- History. Back ();
- }
- } Finally {
- Document. writeln ("start to execute the finally BLOCK statement ")
- }
- }
- </SCRIPT>
2. Another example
Code:
- <Script language = "JavaScript">
- Try {
- Document. writeln ("start to execute the try BLOCK statement ---> ")
- Document. writeln ("no exceptions yet ---> ")
- Alert (prompt ("enter a value :","")))
- } Catch (ERR ){
- Document. writeln ("catch exceptions and start to execute Catch Block statements ---> ");
- Document. writeln ("error name:" + err. Name + "---> ");
- Document. writeln ("error message:" + err. Message + "---> ");
- } Finally {
- Document. writeln ("start to execute the finally BLOCK statement ")
- }
- </SCRIPT>