Handle Errors

Typically, when a MuPAD® procedure encounters an error caused by evaluation of an object, the procedure terminates, the system returns to an interactive level and displays an error message. If you want to avoid terminating a procedure, use the traperror function to catch an error.

The traperror function returns the error number instead of the error itself. Later, you can reproduce the error by using the lasterror function. This function produces the last error that occurred in the current MuPAD session. If you call lasterror inside a statement or a procedure, it terminates that statement or procedure. If you want to display the error message without throwing the error itself, save the error number returned by traperror, and then retrieve the message by using the getlasterror function.

For example, the function f throws an error when sin(π k) is equal to zero:

f := (k) -> 1/sin(PI*k)

f(1)
Error: Division by zero. [_invert]
  Evaluating: f

Suppose you want to compute f for the values - π ≤ k ≤ π increasing the value of k by in each step. To call the function f for all required values, create the for loop. When you try to execute this statement, it terminates as soon as the function f encounters division by zero for the first time. To avoid terminating the for statement, use the traperror function to catch the error. To display the text of that error message without interrupting execution of the for statement, use getlasterror():

for k from -1 to 1 step 1/4 do
  err := traperror(f(k)):
  if err = 0 then
    print(Unquoted, "k = ".expr2text(k), "f = ".expr2text(f(k)))
  else 
    print(Unquoted, "k = ".expr2text(k), getlasterror())
  end_if
end_for
k = -1, [1025, Error: Division by zero. [_invert]]
                 Evaluating: f
k = -3/4, f = -2^(1/2)
k = -1/2, f = -1
k = -1/4, f = -2^(1/2)
k = 0, [1025, Error: Division by zero. [_invert]]
                Evaluating: f
k = 1/4, f = 2^(1/2)
k = 1/2, f = 1
k = 3/4, f = 2^(1/2)
k = 1, [1025, Error: Division by zero. [_invert]]
                Evaluating: f

For errors created with the error function, including your custom error messages, traperror always returns the error code 1028. If an error message with the code 1028 is the last error message that occurred in a MuPAD session, you can retrieve that error message by using lasterror or getlasterror. For example, create the procedure g that computes the factorial of any number less than 10. If you pass a number greater than 10 to this procedure, the procedure reduces the number to 10, and then computes the factorial:

g := proc(n:Type::PosInt)
local test;
begin
  test := proc()
    begin
      if n > 10 then
        error("The number must not exceed 10.")
      end_if
  end_proc:
  if traperror(test(n)) <> 0 then
    g(n - 1)
  else
    n!
  end_if;
end_proc:

Call the procedure with the number 100. During run time, the procedure encounters the error, but does not terminate. It also does not display the error message. Instead, it returns the result:

g(100)

To retrieve and display the error message caught during execution of the procedure g, use the lasterror function:

lasterror()
Error: The number must not exceed 10. [test]

To display the error code and the text of that message, use the getlasterror function:

g(100):
getlasterror()

Was this topic helpful?