To add new messages, define clauses for generate_message_hook/3
.
For instance, to create a new message term indicating that the world
is round, one could choose the message term round_world
and provide
the definition:
generate_message_hook(round_world) --> ['The world is round.'-[],nl]
Please note: The atomnl
is used for breaking the message into lines. Using the format specification~n
(new-line) is strongly discouraged, since applications typically require explicit control over new-lines.
Though this was not obvious from the round_world
example, message
terms parse to a list of Control-Args pairs, where Control is a format
specification string, and Args is the list of arguments for the given
Control string. To illustrate, if we wanted to say that the world was
flat and wet, we could write
user:generate_message_hook(world(X,Y))--> ['The world is ~w and ~w.'-[X,Y],nl]. ?- print_message(help,world(flat,wet)).
Here is an example of how one might implement a new exception,
bad_font
, when the File associated with that font doesn't
exist.
check_font(Font,File):- ( file_exists(File)->true ; raise_exception(bad_font(Font, File)) ). generate_message_hook(bad_font(Font,File)) --> ['Can''t find the file ~w corresponding to font ~w'- [File, Font],nl]).
Instead of using the message facility, you could use an existing error message in this way:
check_font1(Font,File):- ( file_exists(File)->true ; raise_exception(existence_error(check_font1(Font,File),2, file, File,0)) ).