tryCatch<T, E, F extends Object> static method

Result<T, E> tryCatch<T, E, F extends Object>(
  1. T fn(),
  2. E onError(
    1. F e
    )
)

Constructs a new Result from a function that might throw.

Implementation

static Result<T, E> tryCatch<T, E, F extends Object>(
  T Function() fn,
  E Function(F e) onError,
) {
  try {
    return Ok(fn());
  } on F catch (e) {
    return Err(onError(e));
  }
}