Try... catch is used to test Code.
Instance
Try... catch Declaration
How to compile try... catch statements.
Try... catch declaration with confirmation box
Another example of writing the try... catch statement is as follows. Javascript-capture errors
When surfing the internet, we will always see a javascript warning box with a runtime error, and will ask us if we want to debug it ?". Such error messages may be useful to developers, but not to users. When an error occurs, they often choose to leave the site.
This section describes how to capture and process JavaScript error messages, which provides more convenience for the audience.
There are two ways to capture errors on a webpage:
Use try... catch declaration. (Available in ie5 +, Mozilla 1.0, and Netscape 6)
Use the onerror event. This is an old-fashioned method used to capture errors. (Available in Versions later than Netscape 3)
Try... catch Declaration
Try... catch can be used to test errors in the code. The try part contains the code to be run, and the catch part contains the code that runs when an error occurs.
Syntax:
Try
{
// Run the code here
}
Catch (ERR)
{
// Handle errors here
} Note: Try... catch uses lowercase letters. An error occurs when uppercase letters are used.
Instance 1
The following example shows "Welcome guest!" when you click the button! "This message. However, alert () in the message () function is mistakenly written as adddlert (). The following error occurs: <Input type = "button" value = "view message" onclick = "message ()">
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.