Set Up Error Checking and Messages in a Function

Use the same error-messaging mechanism in your functions that system functions use.

Define a function that issues an error message

Define a function that returns a string of n asterisks, with n limited to 80. If n is too large, issue a message and limit the length to 80:

  • Message names always consist of a function name and message identifier separated by a double colon (::).

Define the message

Define the message, with a placeholder for the message argument:

  • Messages can have more than one argument or no arguments at all.

Test the function

No message:

In[3]:=
Click for copyable input
Out[3]=

Error message:

In[4]:=
Click for copyable input
Out[4]=

Notes

Messages gives a list of the messages associated with a function:
In[5]:=
Click for copyable input
Out[5]=
Use Quiet to suppress messages:
In[6]:=
Click for copyable input
Out[6]=