tryy<T> function
The tryy method can be used as a normal try-catch block, or as an expression, i.e. it returns a value.
The tryy allows for assignable try-catch blocks:
final value = tryy(() {
return someMethodThatCouldFail();
}, catches: [
On<SomeException>((err) {
return 1;
})
]);
The tryy can be used in combination with the OrElse method:
final value = tryy(() {
return someMethodThatCouldFail();
}, catches: [
On<SomeException>((err) {
return 1;
})
].orElse(() {
return 1337;
});
Implementation
T? tryy<T>(
T Function() branch, {
List<On> catches = const [],
}) {
try {
return branch();
} catch (err) {
for (final catcher in catches) {
if (catcher.matches(err)) {
final value = (catcher as dynamic).executor(err);
assert(value is T);
return value;
}
}
return null;
}
}