Exception handling method sharing in JS
- 2020-03-30 00:59:02
- OfStack
Js fault-tolerant statement, even if js error does not prompt an error (to prevent the browser bottom right corner of a yellow triangle symbol, or the user experience is not good)
window.onerror=function(){return true;}
The following is to get js exception information, convenient for developers to find the problem
1, try... The catch...
<script type="text/javascript">
var txt=""
function message()
{
try
{
adddlert("Welcome guest!")
}
catch(err)
{
txt=" There is an error on this page. nn"
txt+=" Click ok to continue on this page. n"
txt+=" Click cancel to return to the home page. nn"
if(!confirm(txt))
{
document.location.href="/index.html"
}
}
}
</script>
2, throw
<script type="text/javascript">
var x=prompt(" Please enter the 0 to 10 Number between: ","")
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! This value is too large! ")
if(er == "Err2")
alert(" Error! The value is too small! ")
if(er == "Err3")
alert(" Error! This value is not a number! ")
}
</script>
3, onerror:
<script type="text/javascript">
onerror=handleErr
var txt=""
function handleErr(msg,url,l)
{
txt=" There is an error on this page. nn"
txt+=" Error: " + msg + "n"
txt+="URL: " + url + "n"
txt+=" Line: " + l + "nn"
txt+=" Click ok to continue. nn"
alert(txt)
return true
}
function message()
{
adddlert("Welcome guest!")
}
</script>
Exception handling in js
Try can be used in JavaScript... Catch handles the exception. For example:
Try {foo bar (); } catch (e) {alert(e.ame + ": "+ e.essage); }
Currently, the system exceptions we may obtain mainly include the following 6 types:
If you want to determine the type of exception information, you can do so in catch:
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 (IE only).& cake;
FileName: the wrong fileName (available only to Mozilla).
LineNumber: number of lines that failed (available only to Mozilla).
Message: error message (same as description under IE)
Name: error type.
Number: error code (IE only).
Stack: error stack information like the stack Trace in Java (available only to Mozilla).
Therefore, to better understand the error message, we can change the catch part to the following form:
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 accept it on a catch. Such as:
try {
throw new Date(); // Throws the current time object } catch (e) { alert(e.toLocaleString()); // Displays the current time in a local format
}