finally and Guaranteed Cleanup
Run code whether or not it throws.
Cleanup Must Always Run
Open files and connections must be closed even if code fails. Dart gives you finally for cleanup that always happens. 🧹
The finally Block
A finally block runs after try and catch, no matter what. Success or failure, its code always executes.
try {
work();
} finally {
cleanup();
}All lessons in this course
- throw, try, and catch
- on Clauses and Exception Types
- finally and Guaranteed Cleanup
- Custom Exceptions and rethrow