#try
Evaluates an expression, and if it fails, evaluates a lambda with the exception and returns that result instead.
#try(expression, @e: fallback)Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
expression |
any |
The expression to attempt. |
fallback |
lambda |
Receives the error as @e (with message, exceptionType and, for errors raised by Jolt, a code such as JLT563) and returns the replacement value. |
Returns
Section titled “Returns”any
Examples
Section titled “Examples”Fall back to a default
Section titled “Fall back to a default”{ "stringValue": "not a number", "defaultValue": 0}{ "result": "#try(#valueOf($.stringValue)->#toInteger(), @e: #valueOf($.defaultValue))"}{ "result": 0}Choose a fallback by error code
Section titled “Choose a fallback by error code”{ "name": "Jolt"}{ "result": "#try(#toUpperCase($.nickname), @e: #if(@e.code == 'JLT563', 'no nickname', 'invalid nickname'))"}{ "result": "no nickname"}