Reviewer · Error Handling
Isolate cleanup-code exceptions
Code that runs for bookkeeping, logging, or cleanup purposes (e.g., in a `finally` block, or wrapping an import/export) must never be allowed to raise an exception that replaces or hides the original error. When a secondary operation such as usage accounting or telemetry fails after the primary operation already failed, the caller sees only the secondary...
Code that runs for bookkeeping, logging, or cleanup purposes (e.g., in a finally block, or wrapping an import/export) must never be allowed to raise an exception that replaces or hides the original error. When a secondary operation such as usage accounting or telemetry fails after the primary operation already failed, the caller sees only the secondary exception and loses the real cause and traceback. Wrap such non-essential side-effect code in its own narrow try/except that swallows (or logs) its own failures, so the original exception always propagates intact:
try:
result = agent.run(task)
finally:
if hook is not None:
try:
hook(agent.usage)
except Exception: # accounting must never mask the run result
pass
Conversely, do not use broad try/except as a blanket to paper over real bugs (e.g., swallowing import errors so a module “still loads,” or catching TypeError so broadly that a genuine defect is silently turned into a placeholder string). If something is fundamentally broken, let it fail loudly and specifically — catch only the exception types you expect, and give actionable, exception-specific error messages (e.g., distinguish a UnicodeDecodeError from a generic IOError) rather than reporting a generic failure. The rule of thumb: guard incidental/secondary operations so they never clobber the primary error signal, but do not guard primary operations in a way that hides genuine failures.