This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


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 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?