Finally

To ensure that some code runs whether or not an exception is thrown, usea finally clause. If no catch clause matches the exception, theexception is propagated after the finally clause runs:

  1. try {
  2. breedMoreLlamas();
  3. } finally {
  4. // Always clean up, even if an exception is thrown.
  5. cleanLlamaStalls();
  6. }

The finally clause runs after any matching catch clauses:

  1. try {
  2. breedMoreLlamas();
  3. } catch (e) {
  4. print('Error: $e'); // Handle the exception first.
  5. } finally {
  6. cleanLlamaStalls(); // Then clean up.
  7. }

Learn more by reading theExceptionssection of the library tour.