Main Content

assert

Throw error if condition false

Description

assert(cond) throws an error if cond is false.

example

assert(cond,msg) throws an error and displays the error message, msg, if cond is false.

example

assert(cond,msg,A) displays an error message that contains formatting conversion characters, such as those used with the MATLAB® sprintf function, if cond is false. Each conversion character in msg is converted to one of the values A.

assert(cond,errID,msg) throws an error, displays the error message, msg, and includes an error identifier on the exception, if cond is false. The identifier enables you to distinguish errors and to control what happens when MATLAB encounters the errors.

example

assert(cond,errID,msg,A) includes an error identifier on the exception and displays a formatted error message.

Examples

collapse all

Assert that the value, x, is greater than a specified minimum value.

minVal = 7;
x = 26;

assert(minVal < x)

The expression evaluates as true, and the assertion passes.

Assert that the value of x is between the specified minimum and maximum values.

maxVal = 13;

assert((minVal < x) && (x < maxVal))
Error using assert
Assertion failed.

The expression evaluates as false. The assertion fails and MATLAB throws an error.

Assert that the product of two numbers is a double-precision number.

a = 13;
b = single(42);
c = a*b;

assert(isa(c,'double'),'Product is not type double.')
Error using assert
Product is not type double.

Enhance the error message to display the data type of c.

assert(isa(c,'double'),'Product is type %s, not double.',class(c))
Error using assert
Product is type single, not double.

Use the assert function to test for conditions that should not happen in normal code execution. If the coefficients are numeric, the computed roots should be numeric. A quadratic equation using the specified coefficients and computed roots should be zero.

function x = quadraticSolver(C)

validateattributes(C,{'numeric'},{'size',[1 3]})

a = C(1);
b = C(2);
c = C(3);

x(1) = (-b+sqrt(b^2-4*a*c))/(2*a);
x(2) = (-b-sqrt(b^2-4*a*c))/(2*a);
assert(isnumeric(x),'quadraticSolver:nonnumericRoots',...
    'Computed roots are not numeric')

y1 = a*x(1)^2+b*x(1)+c;
y2 = a*x(2)^2+b*x(2)+c;
assert(y1 == 0,'quadraticSolver:root1Error','Error in first root')
assert(isequal(y2,0),'quadraticSolver:root2Error','Error in second root')

end

Input Arguments

collapse all

Condition to assert, specified as a valid MATLAB expression. This expression must be logical or convertible to a logical. If cond is false, the assert function throws an error. cond can include relational operators (such as < or ==) and logical operators (such as &&, ||, or ~). Use the logical operators and and or to create compound expressions. MATLAB evaluates compound expressions from left to right, adhering to operator precedence rules.

Example: a<0

Example: exist('myfunction.m','file')

Information about the assertion failure, specified as a character vector or string scalar. This message displays as the error message. To format the message, use escape sequences, such as \t or \n. You also can use any format specifiers supported by the sprintf function, such as %s or %d. Specify values for the conversion specifiers via the A1,...,An input arguments. For more information, see Formatting Text.

Note

You must specify more than one input argument with assert if you want MATLAB to convert special characters (such as \t, \n, %s, and %d) in the error message.

Example: 'Assertion condition failed.'

Value that replace the conversion specifiers in msg, specified as a character vector, string scalar, or numeric scalar.

Identifier for the assertion failure, specified as a character vector or string scalar. Use the identifier to help identify the source of the error or to control a selected subset of the errors in your program.

The error identifier includes one or more component fields and a mnemonic field. Fields must be separated with colon. For example, an error identifier with a component field component and a mnemonic field mnemonic is specified as 'component:mnemonic'. The component and mnemonic fields must each begin with a letter. The remaining characters can be alphanumerics (A–Z, a–z, 0–9) and underscores. No white-space characters can appear anywhere in errID. For more information, see MException.

Example: 'MATLAB:singularMatrix'

Example: 'MATLAB:narginchk:notEnoughInputs'

Tips

  • When you issue an error, MATLAB captures information about it and stores it in a data structure that is an object of the MException class. You can access information in the exception object by using try/catch. Or, if your program terminates because of an exception and returns control to the Command Prompt, you can use MException.last.

  • If an assertion failure occurs within a try block, MATLAB does not cease execution of the program. In this case, MATLAB passes control to the catch block.

Extended Capabilities

Version History

Introduced in R2007a

expand all

See Also

|