Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
MessageFailure
Print a message and return a failure object
ResourceFunction["MessageFailure"]["tag","message"] uses "tag" as the Failure tag. | |
ResourceFunction["MessageFailure"]["tag",<|…|>] determines the message text and failure details from the given Association. | |
ResourceFunction["MessageFailure"][symbol::tag] prints the message symbol::tag and returns a corresponding Failure object. | |
ResourceFunction["MessageFailure"][symbol::tag,e1,e2] prints a message and returns a Failure, inserting the values of the ei as needed. |
Details and Options
| "MessageTemplate" | a string template for a message |
| "MessageParameters" | parameters to use for the message template |
Examples
Basic Examples (6)
Print a generic error message and return a Failure object:
| In[1]:= |
| Out[1]= |
Specify the message text:
| In[2]:= |
| Out[2]= |
Specify a tag:
| In[3]:= |
| Out[3]= |
Choose a generic message based on the given tag:
| In[4]:= |
| Out[4]= |
Use Message syntax:
| In[5]:= |
| Out[5]= |
| In[6]:= |
| Out[6]= | ![]() |
Use Failure syntax:
| In[7]:= | ![]() |
| Out[7]= |
| In[8]:= |
| Out[8]= |
Scope (2)
Include additional metadata:
| In[9]:= | ![]() |
| Out[9]= | ![]() |
Specify a different message symbol:
| In[10]:= | ![]() |
| Out[10]= |
Options (8)
MessageFunction (3)
Use a custom message function:
| In[11]:= |
| Out[11]= | ![]() |
Issue the message with ResourceFunctionMessage instead of Message:
| In[12]:= |
| Out[12]= | ![]() |
Define a function that uses the default setting "MessageFunction"→Automatic:
| In[13]:= | ![]() |
By default, Message will be used:
| In[14]:= |
| Out[14]= |
Once converted to a resource function, ResourceFunctionMessage will be automatically used instead:
| In[15]:= |
| Out[15]= |
| In[16]:= |
| Out[16]= |
Use SetOptions to change this globally:
| In[17]:= |
| Out[17]= |
| In[18]:= |
| Out[18]= |
Restore the default setting:
| In[19]:= |
Verbose (2)
By default, only a small number of keys are included in the Failure object:
| In[20]:= |
| Out[20]= | ![]() |
| In[21]:= |
| Out[21]= |
Use "Verbose"→True to include additional metadata used internally by MessageFailure:
| In[22]:= |
| Out[22]= | ![]() |
| In[23]:= |
| Out[23]= | ![]() |
Stack (3)
Include a stack trace in the failure metadata:
| In[24]:= |
| In[25]:= |
| Out[25]= |
View the stack trace:
| In[26]:= |
| Out[26]= | ![]() |
Include a more detailed stack trace in the failure metadata:
| In[27]:= |
| In[28]:= |
| Out[28]= |
View the stack trace:
| In[29]:= |
| Out[29]= | ![]() |
Use StackComplete for even more detail:
| In[30]:= |
| Out[30]= |
| In[31]:= |
| Out[31]= | ![]() |
When using the option setting "Verbose"→True, the stack will also include evaluations made by MessageFailure as well:
| In[32]:= | ![]() |
| In[33]:= |
| Out[33]= |
The stack trace now includes internal evaluations of MessageFailure as well:
| In[34]:= |
| Out[34]= | ![]() |
Applications (4)
Create directives for defining new functions:
| In[35]:= | ![]() |
| In[36]:= | ![]() |
Define a new function using these directives:
| In[37]:= | ![]() |
Test the function:
| In[38]:= |
| Out[38]= |
| In[39]:= |
| Out[39]= |
Undefined inputs are automatically caught as an error:
| In[40]:= |
| Out[40]= |
Properties and Relations (6)
The message can be retrieved from the Failure object:
| In[41]:= |
| Out[41]= | ![]() |
| In[42]:= |
| Out[42]= |
A message can be reissued from the Failure object using Message:
| In[43]:= |
| In[44]:= |
| Out[44]= |
| In[45]:= |
MessageFailure can also reissue the message from the Failure:
| In[46]:= |
| Out[46]= |
MessageFailure can use modern template specifications:
| In[47]:= |
| In[48]:= |
| Out[48]= |
Compare to Message:
| In[49]:= |
Message templates need not be strings:
| In[50]:= | ![]() |
MessageFailure can print messages for Failure objects that use named template parameters:
| In[51]:= | ![]() |
| Out[51]= |
| In[52]:= |
| Out[52]= |
Compare to Message:
| In[53]:= |
| Out[53]= |
Messages with named template slots are temporarily redefined to avoid giving unsupported templates to Message:
| In[54]:= |
| In[55]:= |
| Out[55]= |
Use a message handler to print the current message definition at the time of message generation:
| In[56]:= | ![]() |
| Out[56]= |
The original message definition is restored:
| In[57]:= |
| Out[57]= |
Possible Issues (2)
Using named template slots or template expressions in message definitions gives a template that is not supported by Message:
| In[58]:= |
| In[59]:= |
Use MessageFailure to issue these kinds of messages instead:
| In[60]:= |
| Out[60]= |
Non-string templates won't appear formatted in the resulting Failure object:
| In[61]:= | ![]() |
| Out[61]= | ![]() |
The printed message cannot be retrieved from the failure either:
| In[62]:= |
| Out[62]= |
Related Links
Requirements
Wolfram Language 14.0 (January 2024) or above
Version History
- 1.0.1 – 12 March 2025
- 1.0.0 – 09 August 2021
Related Resources
Related Symbols
License Information
This work is licensed under a Creative Commons Attribution 4.0 International License

![ResourceFunction[
"MessageFailure"]["InvalidRange", <|
"MessageTemplate" -> "Enter a number lower than `Number`", "MessageParameters" -> <|"Number" -> 10|>|>]](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/0da5d0b19f6bbeae.png)
![ResourceFunction["MessageFailure"]["RestrictionFailure",
<|"MessageTemplate" :> Interpreter::numberinterval,
"MessageParameters" -> <|"Min" -> 1, "Max" -> 10, "Input" -> {"100"}|>,
"Interval" -> Interval[{1, 10}],
"Input" -> {"100"},
"Type" -> "Number"|>
]](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/6a3de4dcbf9b65f6.png)

![ResourceFunction["MessageFailure"]["RestrictionFailure",
<|"MessageTemplate" :> Interpreter::numberinterval,
"MessageParameters" -> <|"Min" -> 1, "Max" -> 10, "Input" -> {"100"}|>,
"MessageSymbol" -> MyFunction|>
]](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/704cc7e0c15a91b8.png)


![AddOne // ClearAll;
AddOne::integer = "`1` is expected to be an integer.";
AddOne[x_Integer] := x + 1;
AddOne[other_] := ResourceFunction["MessageFailure"][AddOne::integer, other];](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/78dddded0e577ca5.png)






![rsqrt[x_] := If[TrueQ[x >= 0], Sqrt[x], ResourceFunction["MessageFailure"][rsqrt::nnarg, x, "Stack" -> Full, "Verbose" -> True]];
rsqrt::nnarg = "The argument `1` is not greater than or equal to zero.";](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/701302000e41710b.png)

![BeginDefinition // ClearAll;
BeginDefinition // Attributes = {HoldFirst};
BeginDefinition[s_Symbol] := (Unprotect[s]; ClearAll[s]);](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/222e3fb20dfcde6d.png)
![EndDefinition // ClearAll;
EndDefinition // Attributes = {HoldFirst};
EndDefinition[s_Symbol] := (
expr : s[___] := ResourceFunction["MessageFailure"][
s, <|"MessageTemplate" -> "No definition found for `1`.", "MessageParameters" -> {HoldForm@expr}|>];
Protect@s
);](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/3595b423cdc346cd.png)
![MyFunction // BeginDefinition;
MyFunction[x_Integer] := x + 1;
MyFunction[x_Integer, y_Integer] := x + y;
MyFunction // EndDefinition;](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/6f0ff51ab2be8e8f.png)

![ResourceFunction["MessageFailure"]["MyTag",
<|"MessageTemplate" :> Graphics[{Circle[], Inset[TemplateSlot["ErrorMessage"], {0, 0}]}],
"MessageParameters" -> <|"ErrorMessage" -> "Something broke!"|>|>];](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/1a47b12d53488961.png)

![failure = Failure["InvalidRange", <|
"MessageTemplate" -> "Enter a number lower than `Number`",
"MessageParameters" -> <|"Number" -> 10|>
|>]](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/4eaff0bee5dfe6f1.png)
![Internal`HandlerBlock[{"Message", If[#[[2]], Extract[#, {1, 1}, EchoEvaluation]] &},
ResourceFunction["MessageFailure"][
MySymbol::testing, <|"NamedParameter" -> 123|>]
]](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/01da1c10394be3b3.png)
![failure = ResourceFunction["MessageFailure"][
"AThingBroke",
<|
"MessageTemplate" :> Row[{DateString[], " ", TemplateSlot["ErrorMessage"]}],
"MessageParameters" -> <|"ErrorMessage" -> "Something broke."|>
|>
]](https://cdn.statically.io/img/www.wolframcloud.com/obj/resourcesystem/images/5ab/5ab164bf-292e-4e59-a0c9-bce8509cb49b/3f357aa0cc302642.png)
