Freigeben über


Error Type Extensions

Provides static functions that extend the built-in ECMAScript (JavaScript) Error type by including exception details and support for application-compilation modes (debug or release).

Namespace: None. This type extension is global and not part of a namespace.

Inherits: None

var err = Error.create(message, errorInfo);

Member Extensions

Name

Description

Error.argument Function

Creates an Error object that represents the Sys.ArgumentException exception.

Error.argumentNull Function

Creates an Error object that represents the Sys.ArgumentNullException exception.

Error.argumentOutOfRange Function

Creates an Error object that represents the Sys.ArgumentOutOfRangeException exception.

Error.argumentType Function

Creates an Error object that represents the Sys.ArgumentTypeException exception.

Error.argumentUndefined Function

Creates an Error object that represents the Sys.ArgumentUndefinedException exception.

Error.create Function

Creates an Error object that has optional additional error information.

Error.format Function

Creates an Error object that represents the Sys.FormatException exception.

Error.invalidOperation Function

Creates an Error object that represents the Sys.InvalidOperationException exception.

Error.message Field

Represents the description of the error.

Error.name Field

Represents the name that identifies the error.

Error.notImplemented Function

Creates an Error object that represents the Sys.NotImplementedException exception.

Error.parameterCount Function

Creates an Error object that represents the Sys.ParameterCountException exception.

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.

Remarks

Error extensions are part of the Microsoft Ajax Library. They add functionality to the built-in JavaScript Error object.

Error Types

The Error type extensions generate an Error type with additional fields that represent the exception details. All Error type extensions add at least a name field to identify the exception. You can test the exception's name field to determine what action to take.

The following example shows how to test the Error,name property for the Sys.ArgumentNullException exception.

try{ } 
catch (e) {
    if (e.name === "Sys.ArgumentNullException"){
        // Code here ot handle exception.
    }
}

For more information about the JavaScript object that this type extends, see Error Object in the Language Reference.

Optimizing Client-Script Error Handling for Debug and Release Modes

The ASP.NET AJAX Error object extensions provide additional client-script error handling functionality.

ASP.NET provides debug and release application-compilation modes for working with AJAX client script. This enables you to throw exceptions in debug scripts that are helpful to the debugging process, while minimizing the size of release code to emphasize performance. Debug mode provides additional error-handling features, such as type checking and argument checking, and provides more detailed error messages than release mode does.

For more information, see How to: Enable Debugging for ASP.NET Applications and Debugging and Tracing Ajax Applications Overview.

Example

The following example shows how to create a new Error instance by invoking the create method of the ASP.NET AJAX Error object.

function validateNumberRange(input, min, max) 
{

    // Verify the required parameters were defined.
    if (input === undefined)
    {
        // Throw a standard exception type.
        var err = Error.argumentNull("input", "A parameter was undefined."); 
        throw err;
    }
    else if (min === undefined)
    {
        var err = Error.argumentNull("min", "A parameter was undefined."); 
        throw err;
    }
    else if (max === undefined)
    {
        var err = Error.argumentNull("max", "A parameter was undefined."); 
        throw err;
    }
    else if (min >= max)
    {
        var err = Error.invalidOperation("The min parameter must be smaller than max parameter.");
        throw err;
    }
    else if (isNaN(input))
    {
        msg = "A number was not entered.  ";
        msg += String.format("Please enter a number between {0} and {1}.", min, max);

        var err = Error.create(msg);
        throw err;
    }
    else if (input < min || input > max)
    {
        msg = "The number entered was outside the acceptable range.  ";
        msg += String.format("Please enter a number between {0} and {1}.", min, max);

        var err = Error.create(msg);
        throw err
    }

    alert("The number entered was within the acceptable range.");
}


var input = undefined;
var min = -10;
var max = 10;

// Result: A thrown ErrorArgumentNull exception with the following Error object message:
// "Sys.ArgumentNullException: A parameter was undefined. Parameter name: input"
validateNumberRange(input, min, max);

See Also

Reference

Error Object

new Operator

Error Type Functions

Error Type Fields

Concepts

Debugging and Tracing Ajax Applications Overview

Other Resources

Language Reference