Custom exceptions

For errors that belong to your code, define your own type. Subclass Exception (never BaseException) and end the name in Error. An empty body with a docstring is enough.

Build a small hierarchy: one base class for the module, like BankError, and specific errors under it. Callers then choose how fine to catch: except InsufficientFundsError for one case, except BankError for anything from your module.

A custom error can carry data. Give it an __init__ that stores fields and passes a message to super().__init__(). The caller can then read e.amount instead of parsing the message text.

except clauses are tried in order, so put specific types before their base class. In the code below, swapping the two clauses means the first one never runs.

Run it, then swap the two except clauses and see what changes.