Panic in Scilab

In Scilab, there isn’t a direct equivalent to Go’s panic function. However, we can use error handling and the error function to achieve similar behavior. Here’s how we might implement a similar concept:

function main()
    // We'll use error throughout this site to check for
    // unexpected errors. This is the only program on the
    // site designed to trigger an error.
    error("a problem");
    
    // A common use of error is to abort if a function
    // returns an error value that we don't know how to
    // (or want to) handle. Here's an example of
    // triggering an error if we get an unexpected result when creating a new file.
    [fd, err] = mopen('/tmp/file', 'w');
    if err ~= 0 then
        error(msprintf("Failed to create file: %d", err));
    end
    mclose(fd);
endfunction

main()

Running this program will cause it to trigger an error, print an error message, and exit with a non-zero status.

When the first error in main is triggered, the program exits without reaching the rest of the code. If you’d like to see the program try to create a temp file, comment out the first error call.

--> exec('error_example.sce', -1)
a problem

Note that unlike some languages which use exceptions for handling of many errors, in Scilab it’s common to use error-indicating return values wherever possible, and use the error function for unrecoverable situations.

In Scilab, error handling is typically done using the try-catch construct for recoverable errors, and the error function for unrecoverable errors. The error function in Scilab is somewhat similar to panic in Go, as it immediately stops the execution of the current function and returns control to the command line or the calling environment.