Raise a user-specified exception

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.




error(message) aborts the current procedure, returns to the interactive level, and displays the error message 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 message is printed as an error message.

The printed error message has the form Error: message [name], where name is the name of the procedure containing the call to error. See the examples.

Errors can be caught by the function traperror. If an error occurs while the arguments of traperror are evaluated, control is returned to the procedure containing the call to traperror and not to the interactive level. No error message is printed. The return value of traperror is 1028 when it catches an error raised by error; see Example 2.

The function 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.


Example 1

If the divisor of the following simple division routine is 0, then an error is raised:

mydivide := proc(n, d) begin
  if iszero(d) then
    error("Division by 0")
mydivide(2, 0)
Error: Division by 0 [mydivide]

Example 2

When the error is raised in the following procedure p, control is returned to the interactive level immediately. The second call to print is never executed. Note that the procedure's name is printed in the error message:

p := proc() begin
  print("entering procedure p");
  print("leaving procedure p")

Error: oops [p]

The following procedure q calls the procedure p and catches any error that is raised within p:

q := proc() begin
  print("entering procedure q");
  print("caught error: ", traperror(p()));
  print("leaving procedure q")



The error message: a string

See Also

MuPAD Functions

Was this topic helpful?