A Mathematica package to handle errors when calling a function with wrong arguments.
Thomas Gölles, thomas.goelles@atseda.com
The Wolfram Language is a bit special in the way it handles functions which have been called with wrong arguments. Most built-in functions will give a message and return unevaluated when the arguments are not correct.
I was looking for a better solution which gives me more feedback and makes debugging easier. Therefore, I created the functionArgumentFailure package. Whenever a function is called with wrong arguments a FailureObject is returned and added to the global "failurelist". Each FailureObject informs where the error occurred and the reason for it.
In version 10 the FailureObject has been introduced, and the Function FailureQ has been added in version 10.2. The FailureObject is not often used in the internal function but I think it is ideal for error handling.
The package is compatible and tested with version 12.
In Mathematica go to File > Install ... and install the package.
Needs["functionArgumentFailure`"]
ClearAll[test];
test[a_?StringQ] := 1;
test[args___] := functionArgumentFailure[test, args];
So whenever the pattern of test["some string"] is not matched the functionArgumentFailure is called with 2 arguments. First the name of the failed function and second the arguments. Make sure to use 3 _ for the pattern test, as this allows zero or more arguments.
Here is an example where the function test has been called with not enough arguments:
For further information see Examples.nb and the unit tests in tests/Test.nb