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:
try {
breedMoreLlamas();
} finally {
// Always clean up, even if an exception is thrown.
cleanLlamaStalls();
}
The finally
clause runs after any matching catch
clauses:
try {
breedMoreLlamas();
} catch (e) {
print('Error: $e'); // Handle the exception first.
} finally {
cleanLlamaStalls(); // Then clean up.
}
Learn more by reading theExceptionssection of the library tour.