Chapter 6
Exceptions
An exception is an event that occurs during the execution of a program. An exception requires special treatment. In Picat, an exception is just a term. A built-in exception is an atom or a structure. If it is a structure, the name denotes the type of the exception, and the arguments provide other information about the exception, such as the source, which is the goal or function that raised the exception.
6.1 Built-in Exceptions
Picat throws many types of exceptions. The following are some of the built-in exceptions:1
- domain_error(, ): receives a value that is unexpected in the domain.
- existence_error(, ): tries to use , such as a file, a function, or a solver, that does not exist.
- interrupt: The execution is interrupted by a signal or ctrl-c.
- io_error(, , ): An I/O error with the number and message occurs in .
- load_error(, ): An error occurs while loading the bytecode file named . This error is caused by the malformed bytecode file.
- out_of_bound(, ): tries to access an element of a compound value using the index , which is out of bounds. An index is out of bounds if it is less than or equal to zero, or if it is greater than the length of the compound value.
- out_of_memory(): The system runs out of memory while expanding , which can be: stack_heap, trail, program, table, or findall.
- syntax_error(, ): cannot be parsed into a value that is expected by . For example, read_int() throws this exception if it reads in a string "a" rather than an integer.
- unresolved_function_call(): No rule is applicable to the function call .
- zero_divisor(): divides a number by zero.
- Type_expected(, ): The argument in does not have the expected type or value, where can be var, nonvar, dvar, atom, integer, real, number, list, map, etc.
6.2 Throwing Exceptions
The built-in predicate throw() throws . After an exception is thrown, the system searches for a handler for the exception. If none is found, then the system displays the exception and aborts the execution of the current query. It also prints the backtrace of the stack if it is in debug mode. For example, for the function call open("abc.txt"), the following message will be displayed if there is no file that is named "abc.txt".
*** error(existence_error(source_sink, abc.txt), open)
6.3 Defining Exception Handlers
All exceptions, including those raised by built-ins and interruptions, can be caught by catchers. A catcher is a call in the form:
catch(Goal, Exception, Handler)
which is equivalent to Goal, except when an exception is raised during the execution of Goal that unifies with Exception. When such an exception is raised, all of the bindings that have been performed on variables in Goal will be undone, and Handler will be executed to handle the exception. Note that Exception is unified with a renamed copy of the exception before Handler is executed. Also note that only exceptions that are raised by a descendant call of Goal can be caught.
The call call_cleanup(Call, Cleanup) is equivalent to call(Call), except that Cleanup is called when Call succeeds deterministically (i.e., with no remaining choice point), when Call fails, or when Call raises an exception.
1In the current implementation, exceptions thrown by B-Prolog are not compliant with the documentation.