liveBook · Manning

Finally

A finally clause is an exception-handling block that runs when control leaves an associated protected block, whether normally or exceptionally.

How 2 Manning books explain it

Kotlin in Action, 2E

A finally block is an exception-handling block that executes after a protected block regardless of whether an exception was thrown.

How it is used
Provides guaranteed cleanup for exceptions, resource management, lock release, and coroutine cancellation.
How deep it goes
Demonstrates syntax through parsing, locking, and cancellation examples, then defers routine resource cleanup to use and withLock.
Examples
Uses a BufferedReader parser, a lock, file-reading use, and a cancellable database connection.
What you can do after
Apply it to write cleanup that remains reliable across normal completion, exceptions, and coroutine cancellation.

Read how Kotlin in Action, 2E explains finally →

Python How-To

A finally clause is a block in an exception-handling construct that executes after the associated try or exception-handling clauses, including when control leaves them with a return statement.

How it is used
Runs unconditional follow-up code after exception handling, including completion notifications or cleanup actions.
How deep it goes
Explains clause placement, execution ordering, and interactions with return, break, and continue statements.
Examples
Uses task-string processing, ValueError handling, completion output, and a challenge where finally returns its own value.
What you can do after
Apply it via Python's try statement to guarantee final processing across successful, failed, and early-returning paths.

Read how Python How-To explains finally →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.