Define a function that contains an assertion depending on a temporary result as the computation runs:
This does not test the assertion since assertions are not enabled:
When assertions are enabled with
On, a message is produced:
If you load a package that contains functions with assertions, the name of the package and line number is remembered:
This shows how the line information and package name are stored in the
Assert expression:
Now, if the assertion is triggered, the message tells you the name of the package and the line number where the assertion is located:
You can change the behavior when an assertion fails by assigning to
$AssertFunction. This function throws an exception when an assertion fails:
Here the exception thrown by the assertion function is caught: