Share via


Error.create Function

Creates an Error object that can contain additional error information.

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

Arguments

  • message
    (Optional)An error message string.

  • errorInfo
    (Optional) An instance of a JavaScript object that contains extended information about the error. The object must have a name field that contains a string that identifies the error. The object can also contain fields to fully describe the error.

Return Value

An Error object.

Remarks

Use the create function to create an Error object that can contain additional error information. The message argument enables you to attach information to the error that can be displayed in an application when the error occurs, or to provide details for tracking errors. The errorInfo argument enables you to add error information as fields to the Error object created by the function. This enables you to specify custom error information that more fully describes the error. The JavaScript object that you supply must have a field named name that contains a string that identifies the error. You can provide additional fields of any type and name in the errorInfo object, which is useful for customized exception handling.

Example

The following example shows how to create a new Error object by using the create function. Three errors are created. The first error is an Sys.ArgumentTypeException exception. The second error provides a message string that is passed in the message argument. The third error provides an error message string and an object that is passed in the errorInfo argument. The errorInfo object provides the required name field that identifies the error and an additional field that describes the error.

function throwAnError(input) 
{

  if (input === undefined)
    {
        // Throw a standard exception type.
        var err = Error.argumentNull("input", "A parameter was undefined."); 
        throw err;
    }
    else if (input === "Test1")
    {   
        // Throw a generic error with a message.
        var messageVar = "A test message.";
        var err = Error.create(messageVar)
        throw err;
    }
    else if (input === "Test2")
    {
        // Throw a generic error with a message and associated errorInfo object.
        var messageVar = "This error contains an additional custom errorInfo object";
        var errorInfoObj = { name: "SomeNamespace.SomeExceptionName", someErrorID: "436" };
        var err = Error.create(messageVar, errorInfoObj);
        throw err;
    }
    alert("No error occured.");
}

throwAnError("Test2");

See Also

Reference

Error Type Extensions

Concepts

Debugging and Tracing Ajax Applications Overview

Other Resources

Language Reference