try
Attempts to evaluate an expression and prevents a failure during this evaluation from stopping all subsequent evaluation.
Syntax
-
try(expr[, errexpr])
-
expr
is an expression -
errexpr
is an expression
-
Description
Normally, if an error is raised during evaluation of an expression, all evaluation is immediately terminated and the error message is returned. The try
function allows you to override this behaviour.
try(expr)
evaluates expr
and if expr
returns an error, try
converts this error to null
which is returned to the caller which can then continue its evaluation. If expr
doesn’t raise an error, the value returned by expr
is returned by try
.
try(expr, errexpr)
evaluates expr
and, if no error is raised, returns the value returned by expr
. If an error occurs during evaluation of expr
, errexpr
is evaluated and its value is returned (which might be an error if the evaluation of errexpr
raised one).
Examples
x ≔ 0; try(x ≔ 10 / RandomInt(−5, 6)); x
−5