tryCatch<T> function

Option<T> tryCatch<T>(
  1. T f()
)

Returns the value as a Some if the function succeeds. If it raises an exception, then it will return None.

expect(tryCatch(() => 'hello'), some('hello'));
expect(tryCatch(() => throw 'fail'), none());

Implementation

Option<T> tryCatch<T>(T Function() f) {
  try {
    return some(f());
  } catch (_) {
    return kNone;
  }
}