Error Handling

We briefly discuss how to produce the three possible classes of errors: fatal, warning, and message in Scilab.

Fatal Errors

To signal a fatal error condition in an interface procedure, call error with the appropriate code. The codes can be looked up in SCI/routines/system/error.f.

Here is a code snippet that does this.

      if (ifail .eq. 2) then
          call error(1232)
          return
      endif

If there is no suitable error message, place you own message (length <= 80 chars) in the global variable buf, and call error afterwards.

Warning

The string placed in buf must not be longer than 80 characters.

      if (ier .eq. 6) then
          buf = 'invalid limits'
          call error(32253)
          return
      endif

Sideffect of calling error: The Scilab stack is cleaned up, it put back in the state it was before the interface routine has been entered.

On the Scilab interpreter level an error terminates the evaluation of whatever is currently evaluated (expression, file, or string), unless the trapping of errors has been modified by errcatch. See also: errclear, and iserror.

Warnings

To signal non-fatal error conditions (also known as soft-errors, or warnings), place a negative integer in err2 and call out to display your warning message. Depending on the situation a return may be issued after that. The Scilab stack is not clean up, which means all return values from the interface routine are passed back normally. This is the solution of choice if the user can decide how to proceed based on the return values.

Again, here is a small piece of code for demonstration.

      if (fail .eq. 1) then
          err2 = -6343
          call out('reached table limit')
          return
      endif

On interpreter level it is now mandatory to call iserror after a call to a routine that issues warnings like this. In the user-level error handler the error code must be reset by errclear to allow for further warnings to be signaled.

A typical way of coping with these soft-errors in the interpreter level is shown in Example 7-2.

Example 7-2. Handling of warnings in Scilab

        [z, n, info] = abraxas(a, b, foo, limit)
        if iserror(-19) then
                errclear(-19)
                limit = limit / 2 // make it easier
                [z, n, info] = abraxas(a, b, foo, limit)
                if iserror(-19) then
                        errclear(-19)
                        error(failed even with easy limit');
                end
        end
    

Messages

Messages are the least severe class of errors. Sometimes they are not really errors, but just an additional information that something unexpected is going on. No news is good news.

We have already seen the appropriate subroutine in action. It is out.

      if (iter .gt. 1000) then
          call out('iterating excessively')
      endif