Panic in Chapel

use FileSystem;

proc main() {
    // We'll use halt throughout this site to check for
    // unexpected errors. This is the only program on the
    // site designed to halt.
    halt("a problem");

    // A common use of halt 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
    // halting if we get an unexpected error when creating a new file.
    var file = open("/tmp/file", iomode.cw);
    if (file == none) {
        halt("Failed to create file");
    }
}

In Chapel, the concept of panic is typically represented by the halt statement. The halt statement immediately terminates the program with an error message.

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

When the first halt in main is executed, 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 halt.

$ chpl panic.chpl
$ ./panic
panic.chpl:6: error: halt reached
  panic.chpl:6: thrown here

Note that unlike some languages which use exceptions for handling of many errors, in Chapel it is idiomatic to use error-indicating return values wherever possible. The halt statement is typically used for unrecoverable errors or conditions that should never occur in normal operation.

Chapel also provides other error handling mechanisms, such as throwing and catching errors, which can be used for more graceful error handling in situations where recovery might be possible.