1.使ç¨window.onerroræå®é误å¤çå½æ°ã
å½æé误çæ¶åï¼onerrorä¼è¢«callbackã å½æ个JavaScript blockä¸æå¤ä¸ªscripté误æ¶ï¼ç¬¬ä¸ä¸ªé误触ååï¼åè°callbackï¼ï¼å½åJavascript blockåé¢çscriptä¼è¢«èªå¨Drop忽ç¥æï¼ä¸è¢«æ§è¡ã
å¦ï¼
å¤å¶ä»£ç 代ç å¦ä¸:
<html >
<head>
<title>Test</title>
<script type="text/javascript">
window.onerror = function(message, url, line)
{
alert("Error.\nMessage:"+ message +"\nUrl:" + url + "\nLine:" + line)
return true;
}
</script>
</head>
<body>
<script type="text/javascript">
test();
test();
test();
test();
</script>
<script type="text/javascript">
test();
test();
test();
test();
</script>
</body>
</html>
å¨ä¸é¢çä¾åä¸åªä¼ææ¯ä¸ä¸ªblockä¸ç第ä¸ä¸ªtest();产çerrorã触åwindow.onerroråè°ï¼åé¢çJavascriptä¼è¢«å¿½ç¥æãimg
ä¹æ¯æ onerror < img src="pic.gif" onerror = "javascript:alert("An
error occurred.");"/>ãonerror æ¯æµè§å¨æ¯æç对象ãç±æµè§å¨å³å®æ¯å¦å¯ä»¥ä½¿ç¨ï¼ä¸æ¯DOMæ åã
2.使ç¨Javascriptä¸çtry catch throwå¤çå¼å¸¸ã
Javascriptæ¯æäºtry catch throwï¼Javascriptä¸å®ä¹çå¼å¸¸ï¼
(1)EvalError: An error occurs in the eval() function.
(2)RangeError:
A number value is greater then or less then the number that can be
represented in Javascript(Number.MAX_VALUE and Number.MIN_VAKUE).
(3)ReferenceError: An illegal reference is used.
(4)SyntaxError:
A syntax error occus inside of an eval() function call. All other
syntax error are reorted by the browser and cannot be handled with a
try...catch statement.
(5)TypeError. A variables type is unexpected. 6.URIError. An error ocuurs in the encodeURI() or the decodeURI() function.
å¦ï¼
å¤å¶ä»£ç 代ç å¦ä¸:
<script type="text/javascript">
function CreateError()
{
throw new Error("Created error by custom.");
}
try
{
//throw a error from a function just want to see the call stack in firefox.
CreateError();
}
catch(error)
{
var errorMsg = ("Message: " + error.message + "\n");
if(typeof(error.stack)!=undefined)
{
//FF
errorMsg += ("Line Number: " + error.lineNumber + "\n");
errorMsg += ("File Name: " + error.fileName + "\n");
errorMsg += ("Stack Trace:\n" + error.stack + "\n");
}
else
{
//IE
errorMsg += ("Description: " + error.description + "\n");
errorMsg += ("Number: " + error.number + "\n");
}
alert(errorMsg);
}
finally
{
//alert("End try catch.message from finally block.");
}
</script>
Error.messageæ¯IEåFireFoxé½æ¯æçå±æ§ã
IEæ¯ædescription å numberå±æ§ã
FFæ¯æfileName lineNumber å stack å±æ§ã
ç±äºJavascriptæ¯å¼±ç±»åçè¯è¨ã
æ以å¨catché¨ååªè½catchä¸æ¬¡ï¼ä¸è½åC#è¿æ ·çè¯è¨å¯ä»¥åå¤ä¸ªcatchï¼catchä¸åç±»åçexceptionã
ä½æ¯å¯ä»¥ç¨ instanceof ErrorTypeçæ¹å¼å®ç°ç±»ä¼¼çåè½ã
å¦ï¼
å¤å¶ä»£ç 代ç å¦ä¸:
<script type="text/javascript">
try
{ //Syntax Error
//eval("alert a");
//Custom Error
throw new Error("An error occured.");
}
catch(error)
{
if(error instanceof SyntaxError)
{
alert("Syntax Error");
}
else if(error instanceof EvalError)
{
alert("Eval Error");
}
else if(error instanceof RangeError)
{
alert("Range Error");
}
else if(error instanceof ReferenceError)
{
alert("Reference Error");
}
else if(error instanceof TypeError)
{
alert("Type Error");
}
else if(error instanceof Error)
{
alert("Custon Error");
}
alert(error.message);
}
</script>
注ï¼æµè§å¨ä¸ä¼æåºErrorç±»åçexceptionå¼å¸¸ï¼æ以å¦ææè·å°Errorç±»åçå¼å¸¸ï¼å¯ä»¥ç¡®å®è¿ä¸ªå¼å¸¸æ¯ç¨æ·ä»£ç æåºçï¼ä¸æ¯æµè§å¨æåºçã
Javascriptçassert()
å¤å¶ä»£ç 代ç å¦ä¸:
function assert(bCondition, sErrorMsg) {
if (!bCondition) {
alert(sErrorMsg);
throw new Error(sErrorMsg);
}
}
温馨提示:内容为网友见解,仅供参考