JS exception capture try and catch statements

Source: Internet
Author: User
Tags finally block

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:
  1. <SCRIPT type = "text/JavaScript">
  2. Function Test (){
  3. Try {
  4. Allet (); // alert is wrong
  5. } Catch (ERR ){
  6. Document. writeln ("catch exceptions and start to execute Catch Block statements ---> ");
  7. Document. writeln ("error name:" + err. Name + "---> ");
  8. Document. writeln ("error message:" + err. Message + "---> ");
  9. TXT = "Page error! "
  10. TXT + = "click" OK "to continue viewing the current page ,"
  11. TXT + = "click Cancel to return to the previous page. "
  12. If (! Confirm (txt )){
  13. History. Back ();
  14. }
  15. } Finally {
  16. Document. writeln ("start to execute the finally BLOCK statement ")
  17. }
  18. }
  19. </SCRIPT>

2. Another example

Code:
  1. <Script language = "JavaScript">
  2. Try {
  3. Document. writeln ("start to execute the try BLOCK statement ---> ")
  4. Document. writeln ("no exceptions yet ---> ")
  5. Alert (prompt ("enter a value :","")))
  6. } Catch (ERR ){
  7. Document. writeln ("catch exceptions and start to execute Catch Block statements ---> ");
  8. Document. writeln ("error name:" + err. Name + "---> ");
  9. Document. writeln ("error message:" + err. Message + "---> ");
  10. } Finally {
  11. Document. writeln ("start to execute the finally BLOCK statement ")
  12. }
  13. </SCRIPT>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.