Dart Exceptions: Best Practices
summary
Defines three Dart exception categories (Exception, Error, anything thrown) and recommends only catching Exceptions while letting Errors phone home to crash analytics like Firebase Crashlytics; prefers using rethrow or throwWithStackTrace when wrapping errors to preserve stacks. Advises async/await over chained futures, use typed onError instead of legacy catchError, place whenComplete immediately after future creation, enable very_good_analysis lints, and consider FP Dart’s Either.tryCatch to convert exceptions into values.
AI-written summary · about
chapters (19)
- 0:00Introduction
- 1:06Exception categories
- 3:56Try-catch-finally syntax
- 5:43Catching exceptions
- 7:51Throwing errors
- 10:39Custom exception hierarchies
- 13:08Log and rethrow strategy
- 14:10Repackage and rethrow
- 16:40Clean stack traces
- 18:04Substitute values
- 19:28Lints for exceptions
- 22:38Unawaited futures
- 24:01Errors as state
- 25:28Either for error handling
- 28:17Top-level remote logging
- 29:42Async/await futures
- 31:50Complete and error handling
- 33:15Zones in handling
- 34:17Closing remarks
related talks
17mA closer look into error handling techniques
42mDart null safety in Action
32mError handling: doing it right!
28mError handling: doing it right!
31mHow Many Ways to Say I'm Sorry, Error Handling in React
27mUsing Monads for Elegant Error Handling
30mCat-like Development: Flutter for Efficiency
32mWriting Better Errors
