0PricingLogin
Dart Academy · Lesson

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

  1. throw, try, and catch
  2. on Clauses and Exception Types
  3. finally and Guaranteed Cleanup
  4. Custom Exceptions and rethrow
← Back to Dart Academy