Sys.Debug assert Method

Checks for a condition, and if the condition is false, displays a message and prompts the user to break into the debugger.

Sys.Debug.assert(condition, message, displayCaller);

Arguments

  • condition
    true to continue to execute code; false to display message and break into the debugger.

  • message
    (Optional) The message to display. The default is an empty string ("").

  • displayCaller
    (Optional) true to indicate that the name of the function that is calling assert should be displayed in the message. The default is false.

Remarks

When you call the assert method in your code, express the success of an operation as true or false and use that value for condition. If the operation fails (if condition is false), the assert logic is executed.

The assert method should be used to catch developer errors. To respond to user errors and to run-time error conditions such as network errors or permission failures, throw an exception.

Debugging behavior, requirements, and the output of trace messages vary with different browsers. For more information, see Debugging and Tracing AJAX Applications Overview.

Example

The following example shows a function that calls the assert method if the user-defined variable n is not a positive integer. This is part of a larger example from the Sys.Debug class overview.

function btnAssert_onclick() {
    var n;
    // Insert code intended to set n to a positive integer.
    if (false) n = 3;
    // Assert if n is not greater than 0.
    Sys.Debug.assert(n > 0, "n must be set to a positive integer.");
}
function btnAssert_onclick() {
    var n;
    // Insert code intended to set n to a positive integer. 
    if (false) n = 3;
    // Assert if n is not greater than 0.
    Sys.Debug.assert(n > 0, "n must be set to a positive integer.");
}

See Also

Concepts

Debugging and Tracing AJAX Applications Overview

Reference

Sys.Debug Class