Skip to content

Instantly share code, notes, and snippets.

@PlugFox
Created November 12, 2021 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PlugFox/1c955e7442f8a08e1cebee4d96177648 to your computer and use it in GitHub Desktop.
Save PlugFox/1c955e7442f8a08e1cebee4d96177648 to your computer and use it in GitHub Desktop.
Avoid `throw` in the block `on ... catch (e, st) { }`
/*
* Avoid `throw` in the block `on ... catch (e, st) { }`
* Instead `throw` you must use `Error.throwWithStackTrace`
* https://gist.github.com/PlugFox/1c955e7442f8a08e1cebee4d96177648
* https://dartpad.dev/1c955e7442f8a08e1cebee4d96177648?id=&null_safety=true
*/
void main() {
try {
repository();
} on Object catch (error, stackTrace) {
logError(error, stackTrace);
}
}
/// Where did the original error occur based on the log?
void logError(Object error, StackTrace stackTrace) => print('$error\n$stackTrace');
void repository() {
try {
networkDataProvider();
} on Object {
throw RepositoryException();
}
}
void networkDataProvider() {
try {
networkClient();
} on Object {
throw DataProviderException();
}
}
void networkClient() {
throw 'Some serious problem';
}
class RepositoryException {}
class DataProviderException {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment