Raise a user-specified exception
MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.
MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.
error(message) aborts the current procedure, returns to the interactive level,
and displays the error message
If the error is not caught via
traperror by a procedure that has directly
or indirectly called the current procedure, control is returned to
the interactive level, and the string
printed as an error message.
The printed error message has the form
name is the name of the
procedure containing the call to
error. See the
Errors can be caught by the function
traperror. If an error occurs while the
evaluated, control is returned to the procedure containing the call
not to the interactive level. No error message is printed. The return
it catches an error raised by
error; see Example 2.
error is useful to raise an
error in the type checking part of a user-defined procedure, when this procedure is called
with invalid arguments.
If the divisor of the following simple division routine is
then an error is raised:
mydivide := proc(n, d) begin if iszero(d) then error("Division by 0") end_if; n/d end_proc: mydivide(2, 0)
Error: Division by 0 [mydivide]
When the error is raised in the following procedure
control is returned to the interactive level immediately. The second
p := proc() begin print("entering procedure p"); error("oops"); print("leaving procedure p") end_proc: p()
Error: oops [p]
The following procedure
q calls the procedure
catches any error that is raised within
q := proc() begin print("entering procedure q"); print("caught error: ", traperror(p())); print("leaving procedure q") end_proc: q()
The error message: a string