Issue Warnings and Errors
Issue Warnings
You can issue a warning to flag unexpected conditions detected when running a
program. The warning
function prints a warning message to the
command line. Warnings differ from errors in two significant ways:
Warnings do not halt the execution of the program.
You can suppress any unhelpful MATLAB® warnings.
Use the warning
function in your code to generate a warning
message during execution. Specify the message as the input argument to the
warning
function:
warning('Input must be text')
For example, you can insert a warning in your code to verify the software version:
function warningExample1 if ~strncmp(version, '7', 1) warning('You are using a version other than v7') end
Throw Errors
You can throw an error to flag fatal problems within the program. Use the
error
function to print error messages to the command line.
After displaying the message, MATLAB stops the execution of the current program.
For example, suppose you construct a function that returns the number of
combinations of k
elements from n
elements.
Such a function is nonsensical if k > n
; you cannot choose 8
elements if you start with just 4. You must incorporate this fact into the function
to let anyone using combinations
know of the
problem:
function com = combinations(n,k) if k > n error('Cannot calculate with given values') end com = factorial(n)/(factorial(k)*factorial(n-k)); end
If the combinations
function receives invalid input,
MATLAB stops execution immediately after throwing the error
message:
combinations(4,8)
Error using combinations (line 3) Cannot calculate with given values
Add Run-Time Parameters to Your Warnings and Errors
To make your warning or error messages more specific, insert components of the
message at the time of execution. The warning
function uses
conversion characters that are the same as those used by
the sprintf
function. Conversion characters act as placeholders
for substrings or values, unknown until the code executes.
For example, this warning uses %s
and %d
to
mark where to insert the values of variables arrayname
and
arraydims
:
warning('Array %s has %d dimensions.',arrayname,arraydims)
arrayname = 'A'
and
arraydims = 3
, MATLAB
responds:Warning: Array A has 3 dimensions.
Adding run-time parameters to your warnings and errors can clarify the problems
within a program. Consider the function combinations
from Throw Errors. You
can throw a much more informative error using run-time
parameters:
function com = combinations(n,k) if k > n error('Cannot choose %i from %i elements',k,n) end com = factorial(n)/(factorial(k)*factorial(n-k)); end
combinations(6,9)
Error using combinations (line 3) Cannot choose 9 from 6 elements
Add Identifiers to Warnings and Errors
An identifier provides a way to uniquely reference a warning or an error.
Enable or disable warnings with identifiers. Use an identifying text argument with
the warning
function to attach a unique tag to a
message:
warning(identifier_text,message_text)
For example, you can add an identifier tag to the previous MATLAB warning about which version of software is running:
minver = '7'; if ~strncmp(version,minver,1) warning('MYTEST:VERCHK','Running a version other than v%s',minver) end
Adding an identifier to an error message allows for negative testing. However,
adding and recovering more information from errors often requires working with
MException
objects.
See Also
warning
| lastwarn
| warndlg
| MException