Transferring exceptional control

An operation may encounter a condition that prevents normal completion, such as division by zero or failure to open a file. An exception carries information and redirects control to a handler. Programs can raise exceptions explicitly, and runtimes can generate them for specified errors, allowing an appropriate caller to decide what happens next.

References: [1]

Matching types and propagating outward

Handlers commonly specify accepted exception types. If a suitable handler is absent from the current call, propagation continues outward through callers. Catching a broad category changes which failures are intercepted and may obscure defects that should remain visible. Exception categories and handler boundaries therefore need to be designed together rather than chosen independently.

References: [2]

Cleaning up after either outcome

Files, locks, and connections need cleanup even when execution fails. Python provides finally clauses and context managers to structure that work. Cleanup and recovery remain distinct: releasing resources does not automatically undo earlier changes made to external systems. Multistep updates may additionally require transactions or explicitly designed compensation.

References: [1]

Preserving information for recovery

A handler can choose an alternative, report a clearer problem, or let propagation continue. Reraising and exception chaining help preserve the original cause. Retrying requires considering whether an operation is safe to repeat and whether the failure is temporary; turning every exception into apparent success removes information that callers need to assess completion.

References: [2]