当前位置: 首页 > 图文教程 > 网络编程 > Javascript > Javascript 错误处理的几种方法

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 错误处理的几种方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 295 ::
收藏到网摘: n/a

浏览器不会抛出Error类型的exception异常,所以如果捕获到Error类型的异常,可以确定这个异常是用户代码抛出的,不是浏览器抛出的。 1.使用window.onerror指定错误处理函数。
当有错误的时候,onerror会被callback。 当某个JavaScript block中有多个script错误时,第一个错误触发后(回调callback),当前Javascript block后面的script会被自动Drop忽略掉,不被执行。
如:
复制代码 代码如下:

<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>

在上面的例子中只会有每一个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);
   }
}