Partager via


Error.popStackFrame Function

Updates the fileName and lineNumber properties of an Error instance to indicate where the error was thrown instead of where the error was created. Use this function if you are creating custom error types.

errorInstanceVar.popStackFrame();

Remarks

Some browsers set the fileName and lineNumber fields of an Error instance based on where the Error instance was created. This can be a problem if an Error instance is thrown outside the scope of the function that created it.

Note

The popStackFrame method is called by exceptions that are thrown by the Microsoft Ajax Library. You call the popStackFrame method only if you are defining your own error types.

When you call the popStackFrame method of the Error instance inside the function that created it, the fileName and lineNumber fields of the error instance are updated. Their values are set based on where the error was thrown instead of where the Error instance was created. The popStackFrame function updates the fileName and lineNumber fields of an Error instance based on the next frame in the browser's stack trace. This provides more accurate error information for debugging your code.

Description

The following example shows a function that creates an Error instance and returns it to the code that invoked it, which in turn throws the Error instance. Because the Error instance is thrown outside the function that created it, the popStackFrame method is called from the function that generated the error and before the error is returned.

Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://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>

See Also

Reference

Error Type Extensions

Concepts

Debugging and Tracing Ajax Applications Overview

Other Resources

Language Reference