Document directory
- Syntax:
- Instance 1
- Syntax:
- Instance 1
- Syntax:
- Instance:
Try... catch is used to test errors in code.
Instance
-
Try... catch statement
-
How to compile try... catch statements.
-
Try... catch statement with confirmation box
-
Another example of writing try... catch statements.
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:
- UseTry... catchStatement. (Available in ie5 +, Mozilla 1.0, and Netscape 6)
- UseOnerrorEvent. This is an old-fashioned method used to capture errors. (Available in Versions later than Netscape 3)
Try... catch statement
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 lower-case 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:
We can add try... catch statements so that more appropriate measures can be taken when an error occurs.
The following example uses the try... catch statement to modify the script again. The error occurred because alert () was mistakenly written. But this time, the catch part caught the error and used a piece of prepared code to handle the error. This code will display a custom error message to inform the user of what happened.
<HTML> try{Adddlert ("Welcome guest! ")}catch(err){TXT = "This page has an error. \ N "TXT + =" error Description: "+ err. Description +" \ n "TXT + =" Click OK to continue. \ N "alert (txt )}} </SCRIPT> Instance 2
In the next example, a confirmation box is displayed, asking the user to choose to click the OK button when an error occurs to continue browsing the webpage, or click the cancel button to return to the homepage. 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 will not do anything.
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/" } }}</script>Onerror event
The onerror event will be explained immediately. However, you must first learn how to use the throw statement to create an exception. The throw statement can be used with the try... catch statement.
The role of throw declaration is to create an exception (exception or error ).
Instance
-
Throw statement
-
How to Use throw declaration.
Throw statement
The role declared by throw is to create an exception ). You can use this declaration with the try... catch declaration to control program streams and generate precise error messages.
Syntax:
throw(exception)
Exception can be a string, integer, logical value, or object.
Note: use lower-case letters to write throw. An error occurs when uppercase letters are used!
Instance 1
The following example is used to determine the value of variable X. If the value of X is greater than 10 or less than 0, the error is throw ). After this error is captured by the catch parameter, the custom error message is displayed.
try{ if(x>10) throw "Err1"else if(x<0)throw "Err2"} catch(er){if(er=="Err1") alert("Error! The value is too high")if(er == "Err2") alert("Error! The value is too low") }</script></body>
Onerror is an old-fashioned standard method for capturing JavaScript errors on webpages.
Instance
-
Onerror event
-
How to Use the onerror event to capture errors on the webpage.
Onerror event
We just talked about how to use the try... catch declaration to capture errors on the webpage. Now, we will continue to explain how to use the onerror event for the same purpose.
As long as a script error occurs on the page, an onerror event is generated.
To use the onerror event, you must create a function to handle the error. You can call this function onerror event handler ). This event processor uses three parameters to call: MSG (error message), URL (URL of the page where an error occurs), and line (line where an error occurs ).
Syntax:
onerror=handleErrfunction handleErr(msg,url,l){//Handle the error herereturn true or false}
Whether the browser displays standard error messages depends on the onerror return value. If the returned value is false, an error message is displayed on the console (JavaScript console. Otherwise, no.
Instance:
The following example shows how to use the onerror event to capture errors:
onerror=handleErrvar txt=""function handleErr(msg,url,l){txt="There was an error on this page.\n\n"txt+="Error: " + msg + "\n"txt+="URL: " + url + "\n"txt+="Line: " + l + "\n\n"txt+="Click OK to continue.\n\n"alert(txt)return true}function message(){adddlert("Welcome guest!")}</script>