Skip to content

Instantly share code, notes, and snippets.

@ltOgt
Created July 14, 2020 11:08
Show Gist options
  • Save ltOgt/a48ffdd41c8248e7c9d213c20dc3b276 to your computer and use it in GitHub Desktop.
Save ltOgt/a48ffdd41c8248e7c9d213c20dc3b276 to your computer and use it in GitHub Desktop.
void main() {
dynamic r1 = CustomErrorHandler<dynamic>().execute(
mightThrow: () => []..elementAt(2),
onError: (e) => print("Found Error: <$e>"),
shouldRethrow: false,
);
print(r1);
dynamic r2 = CustomErrorHandler<int>().execute(
mightThrow: () => 1 ~/ 0,
onError: (e) => print("Found Error: <$e>"),
shouldRethrow: false,
);
print(r2);
}
class CustomErrorHandler<T> {
T execute({
T Function() mightThrow,
void Function(dynamic e) onError,
bool shouldRethrow=true,
}) {
try {
return mightThrow();
} catch (e) {
onError(e);
if (shouldRethrow) {
throw e;
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment