Created
November 12, 2021 11:20
-
-
Save PlugFox/1c955e7442f8a08e1cebee4d96177648 to your computer and use it in GitHub Desktop.
Avoid `throw` in the block `on ... catch (e, st) { }`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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