You are here: Start » Programming Tips » Dealing with Domain Errors

Dealing with Domain Errors


Domain Errors stop the program execution when a precondition of a filter is not met. In general it is not possible to predict all possible Domain Errors automatically and this is a responsibility of the user to assure that they will not appear in the final version of the program.

Example Domain Errors:

  • The SelectChannel filter trying to get the second channel of a monochromatic image will raise "[Domain Error] Channel index out of range in SelectChannel.".
  • The DivideIntegers filter when invoked with inB = 0 will raise "[Domain Error] Divisor is equal to zero on input in DivideIntegers.".
  • The RegionMassCenter filter invoked on an empty region will raise "[Domain Error] Input region is empty in RegionMassCenter.".

Domain Errors are reported in the Console window. You can highlight the filter instance that produced a Domain Error by clicking on the link that appears there:

A link to a filter in the Console window.

Dealing with Empty Objects

One of the most common sources of Domain Errors are the filters whose outputs are undefined for the empty object. Let us take the RegionMassCenter filter under consideration: where is the center of an empty region's mass? The answer is: it is undefined. Being unable to compute the result, the filter throws a Domain Error and stops the program execution.

If a program contains such filters and it is not possible to assure that the precondition will always be met, then the special cases have to be explicitly resolved. One way to deal with empty objects is to use one of the guardian filters, which turn empty objects into conditional processing. These filters are: SkipEmptyArray, SkipEmptyRegion, SkipEmptyPath, SkipEmptyProfile, SkipEmptyHistogram and SkipEmptyDataHistogram.

Another, preferred, way is to change the filter variant from Unsafe to OrNil. While the former will result in a Domain Error when the object is empty, the latter will simply skip the execution and assign the Nil value to the filter's outputs. You can find more information on OrNil filter variants here.


An example of a possibly erroneous situation is a use of the ThresholdToRegion filter followed by RegionMassCenter. In some cases, the first filter can produce an empty region and the second will then throw a Domain Error. To prevent that, the SkipEmptyRegion filter should be used before computing the mass center to create an appropriate conditional data flow.

RegionMassCenter throwing a Domain Error on empty input region. The program execution is stopped.

RegionMassCenter properly preceded with SkipEmptyRegion. Conditional processing appears.

Another typical example is when one is trying to select an object having the maximum value of some feature (e.g. the biggest blob). The GetMaximumElement filter can be used for this purpose, but it will throw a Domain Error if there are no objects found (if the array is empty). In this case the SkipEmptyArray filter solves the problem.

Previous: Summary: Common Filters that Everyone Should Know Next: Programming Finite State Machines