Error原生类型

•表示错误对象
–EvalError, URIError, RangeError, etc.
•捕获方式:
–try { …throw new Error(…) } catch(e) { … }
–理论上可以throw出任意对象
•Error对象IE和FireFox公有属性
–message:错误信息


Error浏览器特定属性
•IE:
–description:同message属性
–number:错误编号,只有脚本引擎抛出的错误才有该属性
•FireFox:
–fileName:创建错误的文件
–lineNumber:创建错误对象的行号
–stack:创建错误时的堆栈信息


html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    
<title>Native Error Type</title>
    
<script language="javascript" type="text/javascript" src="Error.js"></script>
</head>
<body>
    
<script language="javascript" type="text/javascript">
        
try
        {
            throwError();
        }
        
catch(e)
        {
            var errorMsg 
= ("Message: " + e.message + "\n");
            
            
if (!e.stack) // IE
            {
                errorMsg 
+= ("Description: " + e.description + "\n");
                errorMsg 
+= ("Number: " + e.number);
            }
            
else
            {
                errorMsg 
+= ("Line Number: " + e.lineNumber + "\n");
                errorMsg 
+= ("File Name: " + e.fileName + "\n\n");
                errorMsg 
+= ("Stack Trace:\n" + e.stack);
            }
            
            alert(errorMsg);
        }
    
</script>
</body>
</html>

Error.js
function throwError()
{
    
throw new Error("Custom Error");
}

function getNiceError()
{
    var e 
= Error.create("Error Message",
        {customMessage : 
"Custom Message"});
    
    e.popStackFrame();
    
return e;
}
posted on 2008-05-04 11:13  一粒沙  阅读(203)  评论(0编辑  收藏  举报