-
Notifications
You must be signed in to change notification settings - Fork 35
ErrorHandling
Information on error handling:
- SWIPL Error generating support library.
- Exception handling in ISO Prolog.
- Printing Messages in SWI-Prolog tutorial.
When throwing errors using throw/1, one should always throw terms of the shape error(Formal, Context) (as opposed to timeouts, aborts and user specific exceptions that are not "errors"), as error/2 is recognised by the debugger to take action under some condition.
An error/2 message is parsed by the following DCG rule in library/prolog_stack.pl:
prolog:message(error(Error, context(Stack, Message))) -->
{ Message \== 'DWIM could not correct goal',
is_stack(Stack, Frames)
},
!,
'$messages':translate_message(error(Error, context(_, Message))),
[ nl, 'In:', nl ],
( {is_list(Frames)}
-> message(Frames)
; ['~w'-[Frames]]
).and rule for translate_message/1 is defined in boot/message.pl which itself uses iso_message/1 to translate Formal error Terms to String messages.
Some ISO formal error Terms are type_error, instantiation_error, or existence_error.
There are also SWI specific errors, translated via swi_message/1 in the same file.
So, if one would like to introduce a new Formal error name, say jpl_state_error, one would need to throw a term of the shape error(jpl_state_error, ...) but also define a DCG rule of the form:
prolog:error_message(jpl_state_error) --> ...See Printing Messages in SWI-Prolog tutorial for more information on how errors are handled in a convenient and flexible way, so that library users can manage those errors the way they want/need.