Javascript 錯(cuò)誤處理的幾種方法
更新時(shí)間:2009年06月13日 23:57:12 作者:
瀏覽器不會(huì)拋出Error類型的exception異常,所以如果捕獲到Error類型的異常,可以確定這個(gè)異常是用戶代碼拋出的,不是瀏覽器拋出的。
1.使用window.onerror指定錯(cuò)誤處理函數(shù)。
當(dāng)有錯(cuò)誤的時(shí)候,onerror會(huì)被callback。 當(dāng)某個(gè)JavaScript block中有多個(gè)script錯(cuò)誤時(shí),第一個(gè)錯(cuò)誤觸發(fā)后(回調(diào)callback),當(dāng)前Javascript block后面的script會(huì)被自動(dòng)Drop忽略掉,不被執(zhí)行。
如:
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
在上面的例子中只會(huì)有每一個(gè)block中的第一個(gè)test();產(chǎn)生error。觸發(fā)window.onerror回調(diào),后面的Javascript會(huì)被忽略掉。img 也支持 onerror < img src="pic.gif" onerror = "javascript:alert("An error occurred.");"/>。onerror 是瀏覽器支持的對(duì)象。由瀏覽器決定是否可以使用,不是DOM標(biāo)準(zhǔn)。
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是弱類型的語(yǔ)言。
所以在catch部分只能catch一次,不能像C#這樣的語(yǔ)言可以寫(xiě)多個(gè)catch,catch不同類型的exception。
但是可以用 instanceof ErrorType的方式實(shí)現(xiàn)類似的功能。
如:
<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>
注:瀏覽器不會(huì)拋出Error類型的exception異常,所以如果捕獲到Error類型的異常,可以確定這個(gè)異常是用戶代碼拋出的,不是瀏覽器拋出的。
Javascript的assert()
function assert(bCondition, sErrorMsg) {
if (!bCondition) {
alert(sErrorMsg);
throw new Error(sErrorMsg);
}
}
當(dāng)有錯(cuò)誤的時(shí)候,onerror會(huì)被callback。 當(dāng)某個(gè)JavaScript block中有多個(gè)script錯(cuò)誤時(shí),第一個(gè)錯(cuò)誤觸發(fā)后(回調(diào)callback),當(dāng)前Javascript block后面的script會(huì)被自動(dòng)Drop忽略掉,不被執(zhí)行。
如:
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
在上面的例子中只會(huì)有每一個(gè)block中的第一個(gè)test();產(chǎn)生error。觸發(fā)window.onerror回調(diào),后面的Javascript會(huì)被忽略掉。img 也支持 onerror < img src="pic.gif" onerror = "javascript:alert("An error occurred.");"/>。onerror 是瀏覽器支持的對(duì)象。由瀏覽器決定是否可以使用,不是DOM標(biāo)準(zhǔn)。
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.
如:
復(fù)制代碼 代碼如下:
<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是弱類型的語(yǔ)言。
所以在catch部分只能catch一次,不能像C#這樣的語(yǔ)言可以寫(xiě)多個(gè)catch,catch不同類型的exception。
但是可以用 instanceof ErrorType的方式實(shí)現(xiàn)類似的功能。
如:
復(fù)制代碼 代碼如下:
<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>
注:瀏覽器不會(huì)拋出Error類型的exception異常,所以如果捕獲到Error類型的異常,可以確定這個(gè)異常是用戶代碼拋出的,不是瀏覽器拋出的。
Javascript的assert()
復(fù)制代碼 代碼如下:
function assert(bCondition, sErrorMsg) {
if (!bCondition) {
alert(sErrorMsg);
throw new Error(sErrorMsg);
}
}
相關(guān)文章
Webpack提取頁(yè)面公共資源的實(shí)現(xiàn)
本文主要介紹了Webpack提取頁(yè)面公共資源的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
微信小程序登錄對(duì)接Django后端實(shí)現(xiàn)JWT方式驗(yàn)證登錄詳解
這篇文章主要介紹了微信小程序登錄對(duì)接Django后端實(shí)現(xiàn)JWT方式驗(yàn)證登錄詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
javascript數(shù)組中的concat方法和splice方法
這篇文章主要介紹了javascript數(shù)組中的concat方法和splice方法,concat方法作用合并數(shù)組,可以合并一個(gè)或多個(gè)數(shù)組,會(huì)返回合并數(shù)組之后的數(shù)據(jù),不會(huì)改變?cè)瓉?lái)的數(shù)組,更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容2022-03-03
TypeScript之元組、數(shù)組及as?const的使用
TypeScript中的元組、數(shù)組和as?const關(guān)鍵字對(duì)于類型安全性和代碼可讀性非常重要,本文主要介紹了TypeScript之元組、數(shù)組及as?const的使用,感興趣的可以了解一下2023-10-10
微信小程序的開(kāi)發(fā)范式BeautyWe.js入門(mén)詳解
這篇文章主要介紹了微信小程序的開(kāi)發(fā)范式BeautyWe.js詳解,它是一套專注于微信小程序的企業(yè)級(jí)開(kāi)發(fā)范式,它的愿景是:讓企業(yè)級(jí)的微信小程序項(xiàng)目中的代碼,更加簡(jiǎn)單、漂亮,需要的朋友可以參考下2019-07-07

