Error原生类型的扩展
•Error.create(message, errorInfo)方法:
–创建新的Error对象
–将Error对象的错误信息属性设为message
–将errorInfo上的信息附加到Error对象
•Error.prototype.popStackFrame()方法:
–为Error对象整理出更优雅直观的信息(lineNumber, stack)
–对于IE无效
–如果一个方法仅仅是返回Error对象而不是抛出对象,则在返回前应该调用该方法
aspx
Error.js
–创建新的Error对象
–将Error对象的错误信息属性设为message
–将errorInfo上的信息附加到Error对象
•Error.prototype.popStackFrame()方法:
–为Error对象整理出更优雅直观的信息(lineNumber, stack)
–对于IE无效
–如果一个方法仅仅是返回Error对象而不是抛出对象,则在返回前应该调用该方法
aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Error Type Extensions</title>
<script language="javascript" type="text/javascript" src="Error.js"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
please view the page in FireFox
<script language="javascript" type="text/javascript">
try
{
throw getNiceError();
}
catch(e)
{
var errorMsg = ("Message: " + e.message + "\n");
errorMsg += ("Line Number: " + e.lineNumber + "\n");
errorMsg += ("File Name: " + e.fileName + "\n\n");
errorMsg += ("Stack Trace:\n" + e.stack);
alert(errorMsg);
}
</script>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Error Type Extensions</title>
<script language="javascript" type="text/javascript" src="Error.js"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
please view the page in FireFox
<script language="javascript" type="text/javascript">
try
{
throw getNiceError();
}
catch(e)
{
var errorMsg = ("Message: " + e.message + "\n");
errorMsg += ("Line Number: " + e.lineNumber + "\n");
errorMsg += ("File Name: " + e.fileName + "\n\n");
errorMsg += ("Stack Trace:\n" + e.stack);
alert(errorMsg);
}
</script>
</form>
</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;
}
{
throw new Error("Custom Error");
}
function getNiceError()
{
var e = Error.create("Error Message",
{customMessage : "Custom Message"});
e.popStackFrame();
return e;
}