|Did you know ...||Search Documentation:|
|Pack onepointfour_basics -- prolog/safe_format.pl|
format/3 and format/2 are demanding in what they require as arguments. They throw an exception if there is a mismatch between the placeholders count and the count of elements in Args, or if an element in Args is a mismatch for a placeholder in Msg. This can lead to exceptions at inopportune times, especially if the Msg or Args arguments are built dynamically and code lacks sufficient coverage.
Use this predicate to make format/3 generate ResultString (always an SWI-Prolog string) from Msg and Args. If an exception is thrown from the call to format/3, it is caught and a replacement message is generated in ResultString.
?- safe_format("Hello ~d",,Result). Result = "Hello 7889". ?- safe_format("Hello ~d",[hello],Result). Result = "Exception in format/3 with format string <Hello ~d> and args <hello>". ?- safe_format("Open the ~s.",["pod bay doors","HAL"],Result). Result = "Exception in format/3 with format string <Open the ~s.> and args <\"pod bay doors\">,<\"HAL\">".