site-www
site-www copied to clipboard
Fix Exception exercise in 'Dart cheatsheet codelab' page
Page URL
https://dart.dev/codelabs/dart-cheatsheet/
Page source
https://github.com/dart-lang/site-www/tree/main/./src/content/codelabs/dart-cheatsheet.md
Describe the problem
This is a bit of a nitpick: The solutions provided for the exercise in the section Exceptions unaccountably gives the Exception's actual runtime type to the logger in one case, but not in the other:
void tryFunction(VoidFunction untrustworthy, Logger logger) {
try {
untrustworthy();
} on ExceptionWithMessage catch (e) {
logger.logException(e.runtimeType, e.message); // passes runtimeType here
} on Exception {
logger.logException(Exception); // but only passes type "Exception" here
} finally {
logger.doneLogging();
}
}
This is also in contradiction with the exercise statement, which is:
- If
untrustworthy()
throws anExceptionWithMessage
, calllogger.logException
with the exception type and message (try usingon
andcatch
). - If
untrustworthy()
throws anException
, calllogger.logException
with the exception type (try usingon
for this one).
Expected fix
Just stay on target:
void tryFunction(VoidFunction untrustworthy, Logger logger) {
try {
untrustworthy();
} on ExceptionWithMessage catch (e) {
logger.logException(e.runtimeType, e.message);
} on Exception {
logger.logException(e.runtimeType);
} finally {
logger.doneLogging();
}
}
The above also passes the check.
Additional context
No response
I would like to fix this problem.
- [ ] I will try and fix this problem on dart.dev.