Compartilhar via


Error.argumentNull Function

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

var err = Error.argumentNull(paramName, message);

Arguments

  • paramName
    (Optional) The name of the parameter as a string that caused the exception. If paramName is null, a default message is used.

  • message
    (Optional) An error message string. The value can be null.

Return Value

An Error object that has name and paramName fields added to the Error type definition.

Remarks

The argumentNull function lets you communicate that an exception has occurred when a function or method was invoked and when an argument has an invalid value of null. The message parameter lets you specify an error message. The paramName lets you specify the name of the parameter whose value was null.

Example

The following example shows how to create, throw, and catch a Sys.ArgumentNullException error by using the argumentNull function.

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 Type Extensions

Concepts

Debugging and Tracing Ajax Applications Overview

Other Resources

Language Reference