js fault-tolerant statement, even if js errors occur, no error will be prompted (to prevent a yellow triangle symbol in the lower right corner of the browser, otherwise the user experience will be poor)
Copy the code code as follows:
window.onerror=function(){return true;}
The following is to obtain js exception information to facilitate developers to find problems
1,try...catch...
Copy the code code as follows:
<script type="text/javascript">
var txt=""
function message()
{
try
{
adddlert("Welcome guest!")
}
catch(err)
{
txt="There is an error on this page./n/n"
txt+="Click "OK" to continue viewing this page,/n"
txt+="Click "Cancel" to return to the homepage./n/n"
if(!confirm(txt))
{
document.location.href="/index.html"
}
}
}
</script>
2,throw
Copy the code code as follows:
<script type="text/javascript">
var x=prompt("Please enter a number between 0 and 10:","")
try
{
if(x>10)
throw "Err1"
else if(x<0)
throw "Err2"
else if(isNaN(x))
throw "Err3"
}
catch(er)
{
if(er=="Err1")
alert("Error! The value is too large!")
if(er == "Err2")
alert("Error! The value is too small!")
if(er == "Err3")
alert("Error! The value is not a number!")
}
</script>
3,onerror:
Copy the code code as follows:
<script type="text/javascript">
onerror=handleErr
var txt=""
function handleErr(msg,url,l)
{
txt="There is 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>
Exception handling in js
You can use try...catch in JavaScript for exception handling. For example:
try { foo.bar();} catch (e) { alert(e.name + ": " + e.message);}
At present, the system exceptions we may get mainly include the following 6 types:
EvalError: raised when an error occurs executing code in eval()
RangeError: raised when a numeric variable or parameter is outside of its valid range
ReferenceError: raised when de-referencing an invalid reference
SyntaxError: raised when a syntax error occurs while parsing code in eval()
TypeError: raised when a variable or parameter is not a valid type
URIError: raised when encodeURI() or decodeURI() are passed invalid parameters
The above six exception objects all inherit from the Error object. They all support the following two construction methods:
new Error();new Error("Exception information");
The method of manually throwing an exception is as follows:
Copy the code code as follows:
try {
throw new Error("Whoops!");}
catch (e) {
alert(e.name + ": " + e.message);}
If you want to determine the type of exception information, you can do it in catch:
Copy the code code as follows:
try {
foo.bar();
} catch (e) {
if (e instanceof EvalError) {
alert(e.name + ":" + e.message);
} else if (e instanceof RangeError) {
alert(e.name + ": " + e.message); }
//etc
}
Error has the following main properties:
description: Error description (only available in IE).
fileName: The file name of the error (only available in Mozilla).
lineNumber: The number of lines in error (only available in Mozilla).
message: error message (same as description under IE)
name: error type.
number: error code (only available in IE).
stack: Error stack information like Stack Trace in Java (only available in Mozilla).
Therefore, in order to better understand the error message, we can change the catch part to the following form:
Copy the code code as follows:
try {
foo.bar();
} catch(e) {
if (browserType != BROWSER_IE) {
alert("name: " + e.name + "message: " + e.message + "lineNumber: " + e.lineNumber + "fileName: " + e.fileName + "stack: " + e.stack);
} else {
alert("name: " + e.name + "errorNumber: " + (e.number & 0xFFFF) + "message: " + e.message "); } } "
The throw command in JavaScript can actually throw any object, and we can receive this object in catch. For example:
Copy the code code as follows:
try {
throw new Date(); // Throws the current time object } catch (e) { alert(e.toLocaleString()); // Use local format to display the current time
}