site-www icon indicating copy to clipboard operation
site-www copied to clipboard

Fix Exception exercise in 'Dart cheatsheet codelab' page

Open dtonhofer opened this issue 4 months ago • 0 comments

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 an ExceptionWithMessage, call logger.logException with the exception type and message (try using on and catch).
  • If untrustworthy() throws an Exception, call logger.logException with the exception type (try using on 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.

dtonhofer avatar Mar 02 '24 13:03 dtonhofer