Edit

Share via


Flow control functions for Bicep

Bicep provides the fail function for influencing execution flow.

The fail function is primarily used for enforcing constraints and terminating evaluation based on logical conditions. It usually works within short-circuiting expressions like ?? (coalesce operator) and ?: (ternary operator). For more information, see Logical operators.

fail

fail(arg1)

The fail function is useful for enforcing constraints in expressions, but it can only be used within short-circuiting functions—operations that stop execution as soon as the outcome is determined.

Namespace: sys.

Parameters

Parameter Required Type Description
arg1 Yes string A descriptive error message that explains why the failure occurred.

Examples

The following example shows how to use fail.

anObjectParameter.?name ?? anObjectParameter.?id ?? fail('Expected anObjectParameter to have either a .name or a .id property')

Here, the coalesce operator (??) ensures that if .name exists, execution stops immediately. If .name is null or undefined, .id is checked. If both are missing, fail is triggered, preventing further execution.

x != 0 ? y / x : fail('x cannot be zero because it will be used as a divisor')

In this case, the ternary operator (? :) checks if x is nonzero before performing division. If x is 0, fail is invoked, stopping execution before an invalid operation occurs.

Next steps