FUNCTION

try_call_catch

function try_call_catch<T>(fn: () -> T): try_call_result<T>

Safely call a function that may fail (i.e. that may throw an exception), returning a result that can be inspected for errors.

Accepts 0-ary function references, i.e. references to functions of type () -> T, and returns a try_call_result<T> which wraps either the return value of the function, or error information if the function threw an exception.

Only exceptions of type require are caught, and any other exceptions are re-thrown. Use try_call() variants if you want to handle all exceptions.

Changes to the database that occur during the call are rolled back when an exception is thrown.

Examples:

function fails(): integer {
    require(false, "This fails");
    return 0;
}
function succeeds(): integer { return 17; }
try_call_catch(fails(*)) // returns a try_call_result containing the error message
try_call_catch(succeeds(*)) // returns a try_call_result containing 17

// `fn` can also be written as a lambda:
try_call_catch(() -> { require(false, "This fails"); 0 }) // try_call_result containing the error message
try_call_catch(() -> 17) // returns a try_call_result containing 17

Parameters

  • fn — the function to be called

Returns

a try_call_result<T> containing either the return value of fn or error information

Since

0.14.16

© 2026 Chromia