5. Use add_note for extra exception context
Use add_note() to attach extra debugging context instead of overloading the exception message itself.
This separates the original exception message from the local debugging context, which preserves the base error while still giving operators more clues. It is especially helpful when the same exception type can arise from many similar call sites.
Note
Python 3.11+
5.1. Don’t do this
1try:
2 process_invoice(invoice_id)
3except ValueError as exc:
4 raise ValueError(f'invoice failed: {invoice_id}') from exc
5.2. Do this
1try:
2 process_invoice(invoice_id)
3except ValueError as exc:
4 exc.add_note(f'invoice_id={invoice_id}')
5 raise