abort/0 does not close any files which may have been opened. If the program under execution is doing file manipulation using see/1 and tell/1, then unexpected behavior may occur after the program is aborted and restarted, unless the user manually closes the files.
Aborting closes all incomplete tables (those which may not have a complete set of answers). Closed tables are unaffected, even if the tables were created during the aborted computation.
abort/0 is implemented by throwing the term '_$abort_ball'/0. Implementations that wish to override the default behavior of abort/0 must call a top-level goal via catch/1 and ensure that '_$abort_ball'/0 is handled.
The following predicate userdiv/2 is designed to be called with the first argument instantiated to a number. A second number is then read from a console, and the first number is divided by the second, and unified with the second argument of userdiv/2. By using catch/3 and throw/1 together the various types of errors can be caught.
userdiv(X,Ans):- catch(userdiv1(X,Ans),mydiv1(Y),handlefoo(Y,X)). userdiv1(X,Ans):- (number(X) -> true; throw(mydiv1(exception1))), write('Enter a number: '),read(Y), (number(Y) -> true ; throw(mydiv1(exception2(Y)))), (Y =;= 0 -> throw(mydiv1(exception3(Y))); true), Ans is X/Y.
The behavior of this program on some representative inputs is shown below.
| ?- userdiv(X,Y). userdiv/1 called with non-numeric numerator: _h76 X = _h76 Y = _h90 yes | ?- userdiv(3,Y). Enter a number: foo. in userdiv/1 a non-numeric denominator was entered: foo Y = _h84 yes | ?- userdiv(3,Y). Enter a number: 0. in userdiv/1 a denominator of 0 was entered. Y = _h84 yes | ?- userdiv(3,Y). Enter a number: 2. Y = 1.5000 yes
The actions of catch/3 and throw/1 resemble that of the Prolog cut in that they remove choice points that lie between a call to throw/1 and the matching catch/3 that serves as its ancestor. However, if this process encounters a choice point for an incomplete table, execution is aborted to the top user level.
All exceptions that occur during the execution of an XSB program can be caught. Below is a list of some of the more important exceptions: