ResTTry Method

When IsOk executes action(val) in a try-catch block: returns Ok if the process succeeds; Err if it throws. Does not do anything and returns the Err when this IsErr.
C#
static Res<User> TryGetUser() { .. }
static void PutUserToDb(User user) {
    // method that writes the user to a database table
    // might fail and throw!
}

Res<User> user = TryGetUser().Try(PutUserToDb);
// equivalently:
Res<User> user = TryGetUser().Try(() => PutUserToDb());

// user will be:
// - Err(called on Err) if () returns Err.
// - Err(relevant error message) if () returns Ok(user) but database action throws an exception.
// - Ok(user) if () returns Ok(user), further the action is operated successfully;

// it provides a shorthand for the following verbose/unpleasant version:
Res<User> user = TryGetUser();
if (user.IsOk)
{
    try
    {
        PutUserToDb(user.Unwrap());
    }
    catch (Exception e)
    {
        user = Err<User>("db-operation failed, check the exception message: " + e.Message);
    }
}

Definition

Namespace: Orx.Fun.Result
Assembly: Orx.Fun.Result (in Orx.Fun.Result.dll) Version: 1.3.0+344c8bdd6f720ccfb2d8db7c61b76cf02be18f5f
C#
public Res Try(
	Action<T> action,
	string name = ""
)

Parameters

action  ActionT
Action to be called with the underlying value in try-catch block when Ok.
name  String  (Optional)
Name of the operation/action; to be appended to the error messages if the action throws. Omitting the argument will automatically be filled with the action's expression in the caller side.

Return Value

Res

See Also