assert - Throw error if condition false - MATLAB (original) (raw)
Throw error if condition false
Syntax
Description
assert([cond](#bujv3e0-1-cond))
throws an error if cond
is false.
assert([cond](#bujv3e0-1-cond),[msg](#bujv3e0-1-msg))
throws an error and displays the error message, msg
, if cond
is false.
assert([cond](#bujv3e0-1-cond),[msg](#bujv3e0-1-msg),[A](#mw%5F61b7bd14-9b03-47a6-bfe9-9074cd843377))
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 valuesA
.
assert([cond](#bujv3e0-1-cond),[errID](#bujv3e0-1-msgID),[msg](#bujv3e0-1-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.
assert([cond](#bujv3e0-1-cond),[errID](#bujv3e0-1-msgID),[msg](#bujv3e0-1-msg),[A](#mw%5F61b7bd14-9b03-47a6-bfe9-9074cd843377))
includes an error identifier on the exception and displays a formatted error message.
Examples
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-4ac))/(2a); x(2) = (-b-sqrt(b^2-4ac))/(2a); assert(isnumeric(x),'quadraticSolver:nonnumericRoots',... 'Computed roots are not numeric')
y1 = ax(1)^2+bx(1)+c; y2 = ax(2)^2+bx(2)+c; assert(y1 == 0,'quadraticSolver:root1Error','Error in first root') assert(isequal(y2,0),'quadraticSolver:root2Error','Error in second root')
end
Input Arguments
Condition to assert, specified as a valid MATLAB expression. This expression must be logical or convertible to a logical. If cond
is false, theassert
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 fieldcomponent
and a mnemonic fieldmnemonic
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, seeMException.
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 usingtry/catch
. Or, if your program terminates because of an exception and returns control to the Command Prompt, you can useMException.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 thecatch
block.
Extended Capabilities
Usage notes and limitations:
- Generates specified error messages at compile time only if all input arguments are constants or depend on constants. Otherwise, generates specified error messages at run time.
- If called with more than 1 argument, has no effect in standalone code even when run-time error detection is enabled. See Generate Standalone C/C++ Code That Detects and Reports Run-Time Errors (MATLAB Coder).
- To use the
assert
function to specify properties of primary function inputs or set preconditions on primary function inputs, see Rules for Using assert Function (MATLAB Coder).
The assert
function supports GPU array input with these usage notes and limitations:
- This function accepts GPU arrays, but does not run on a GPU.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2007a
When an assertion fails, the error thrown includes the specific assertion that failed and the location in the code.