更新:2007 年 11 月
更新 Error 实例的 fileName 和 lineNumber 属性,可以指示引发错误的位置而不是创建错误的位置。如果要创建自定义错误类型,请使用此函数。
errorInstanceVar.popStackFrame();
备注
一些浏览器根据创建 Error 实例的位置设置 Error 实例的 fileName 和 lineNumber 字段。如果在创建 Error 实例的函数范围以外引发该实例,这可能出现问题。
说明: |
|---|
popStackFrame 方法由 Microsoft AJAX Library 引发的异常调用。仅当定义您自己的错误类型时才应调用 popStackFrame 方法。 |
当您在创建 Error 实例的函数内调用该实例的 popStackFrame 方法时,将更新该错误实例的 fileName 和 lineNumber 字段。这些字段的值根据引发错误的位置而不是创建 Error 实例的位置设置。popStackFrame 函数根据浏览器堆栈跟踪中的下一帧更新 Error 实例的 fileName 和 lineNumber 字段。这将为调试代码提供更准确的错误信息。
说明
下面的示例演示的函数创建一个 Error 实例,将该实例返回到调用它的代码,代码随后引发 Error 实例。因为 Error 实例在创建它的函数以外引发,所以 popStackFrame 方法在错误返回前从生成错误的函数调用。
代码
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
<script type="text/javascript">
// Register classes to test.
Type.registerNamespace('Samples');
Samples.A = function(){}
Samples.A.registerClass('Samples.A');
Samples.B = function()
{
Samples.B.initializeBase(this);
}
Samples.B.registerClass('Samples.B');
// Test the type, create an Error in a function and return it.
function validate(testType, expectedType) {
if (!testType.isInstanceOfType(expectedType))
{
var e = Error.create("Invalid type.");
// Ensure that the Error tracks where it is
// thrown rather than where it was created.
e.popStackFrame();
return e;
}
}
// Cause validate() to create and return an error.
var a = new Samples.A();
var b = new Samples.B();
var err = validate(Samples.A, b);
if (err)
{
throw err;
}
alert("No error occured.");
</script>
</form>
</body>
</html>
说明: