In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the usage of "Try … Catch sentence in JavaScript". In daily operation, I believe that many people Try in JavaScript. There are doubts about the usage of Catch sentences. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods, hoping to help you answer the doubts about "the usage of Try … Catch sentences in JavaScript"! Next, please follow the editor to study!
Article catalogue
Syntax:
JavaScript Try...Catch statement
Previous Page
Next Page
The purpose of try...catch is to test for errors in the code.
Example
Try...catch statement
How to write try...catch statements.
Try...catch statement with confirmation box
Another example of writing try...catch statements.
JavaScript-catch error
When we surf the Internet, we always see a Javascript alert box with a runtime error and ask us, "do you want to debug?" . Error messages like this may be useful to developers, but not necessarily to users. When errors occur, they often choose to leave the site.
This section shows you how to capture and handle error messages from Javascript so as to provide more convenience to the audience.
There are two ways to catch errors in a web page:
Use the try...catch statement. (available in IE5+, Mozilla 1.0, and Netscape 6)
Use the onerror event. This is the old-fashioned method for catching errors. (versions later than Netscape 3 are available)
Note: chrome, opera, and safari browsers do not support onerror events.
Try...Catch statement
Try...catch can test for errors in your code. The try section contains the code that needs to be run, while the catch section contains the code that runs when the error occurs.
Syntax: try {/ / run code here} catch (err) {/ / handle errors} Note: try...catch uses lowercase letters. Capital letters can go wrong.
Example 1
The following example was originally used to display "Welcome guest!" when the user clicks the button. This news. However, alert () in the message () function is miswritten as adddlert (). Then the error occurred:
Function message () {adddlert ("Welcome guest!")}
TIY
Example 2
The next example displays a confirmation box that allows the user to choose whether to click the OK button to continue browsing the web page when an error occurs or to click the cancel button to return to the home page. If the return value of the confirm method is false, the code redirects the user to another page. If the return value of the confirm method is true, the code does nothing.
Var txt= "" function message () {try {adddlert ("Welcome guest!")} catch (err) {txt= "There was an error on this page.\ n\ n" txt+= "Click OK to continue viewing this page,\ n" txt+= "or Cancel to return to the home page.\ n\ n" if (! confirm (txt)) {_ document.location.href= "http://www.w3school.com.cn/"}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.