Wolfram/ ErrorTools

Prototype implementation of next-generation error handling for Wolfram Language, from the Error Handling Working Group (EHWG)

Contributed by: Error Handling Working Group

This paclet contains experimental prototypes of new functions and frameworks for robustly and ergonomically reporting and handling of error conditions within Wolfram Language code.

This paclet is a product of the Error Handling Working Group.

Installation Instructions

To install this paclet in your Wolfram Language environment, evaluate this code:
PacletInstall["Wolfram/ErrorTools"]

Details

The functionality in this paclet is a collection of prototype implementations intended for experimentation and rapid iteration. At present, none of this functionality should be depended on for stability between versions.

Paclet Guide

Examples

Basic Examples (4) 

Register a new error tag:

In[1]:=
CreateErrorType[MyError, {}]
Out[1]=

Create a new Failure[..] with the specified tag:

In[2]:=
CreateFailure[MyError, "An error occurred at ``", DateString[]]
Out[2]=

Construct and throw a Failure[..] as an exception, without handling the failure:

In[3]:=
Raise[MyError, "Uh oh!"]
Out[3]=

Handle the raised Failure:

In[4]:=
Handle[Raise[MyError, "Uh oh!"], FailurePattern[MyError]]
Out[4]=

Scope (2) 

Define a function that can raise multiple types of failure:

In[5]:=
foo[cond_] := If[cond,
  	Raise[MyError, "Uh oh!"]
  	,
  	Raise[OtherError, "Uh oh!"]
  ]

Handle any type of failure:

In[6]:=
Handle[foo[True], _Failure]
Out[6]=
In[7]:=
Handle[foo[False], _Failure]
Out[7]=

Handle only Failure's with a specific tag:

In[8]:=
Handle[foo[True], {
  	f : FailurePattern[MyError] :> Print["Failed due to MyError: ", f]
  }]

Handle does not catch a Failure if no handler rule matches it:

In[9]:=
Handle[foo[False], {
  	f : FailurePattern[MyError] :> Print["Failed due to MyError: ", f]
  }]
Out[9]=

Define a function that throws an error when called with unexpected arguments:

In[10]:=
SetFallthroughError[SayHello]
In[11]:=
SayHello[name_?StringQ] := Print["Hello, ", name, "!"]

Say hello with a valid string argument:

In[12]:=
SayHello["John"]

Try to say hello with an invalid argument:

In[13]:=
SayHello[2 + 2]
Out[13]=

Disclosures

Compatibility

Wolfram Language Version 13.2

Version History

  • 0.1.2 – 05 October 2023
  • 0.1.1 – 25 August 2023
  • 0.1.0 – 24 August 2023

License Information

MIT License

Paclet Source