Either<L, R>.tryCatch constructor

Either<L, R>.tryCatch(
  1. R run(),
  2. L onError(
    1. Object o,
    2. StackTrace s
    )
)

Try to execute run. If no error occurs, then return Right. Otherwise return Left containing the result of onError.

Implementation

factory Either.tryCatch(
    R Function() run, L Function(Object o, StackTrace s) onError) {
  try {
    return Either.of(run());
  } catch (e, s) {
    return Either.left(onError(e, s));
  }
}