Reviewer · Error Handling
Avoid masking exceptions
Secondary or cleanup logic must never hide the error that actually caused a failure, and exception handling scope must be as narrow as the code's intent.
Secondary or cleanup logic must never hide the error that actually caused a failure, and exception handling scope must be as narrow as the code’s intent.
- Code that runs in a
finallyblock or other cleanup/bookkeeping path (usage accounting, telemetry hooks, logging) must be wrapped in its owntry/exceptso it can never replace the primary exception. For example, calling a usage hook infinallywithout guarding it can turn a meaningfulReasoningAgentExecutorErrorinto an unrelatedAttributeError, losing the real cause and traceback:
finally:
if swarm is not None:
try:
self._add_usage(swarm)
except Exception: # accounting must never mask the run result
pass
-
Catch only the exception types you actually intend to handle. Avoid
except BaseException(or bareexcept:) in generic control-flow drivers, since it also swallowsKeyboardInterrupt,SystemExit, and, in async code,asyncio.CancelledError— this can prevent task cancellation from propagating and leave loops running when they should have stopped. Preferexcept Exceptionand let truly exceptional signals propagate. -
Do not add blanket try/except blocks purely to suppress failures that indicate a real bug (e.g., wrapping module imports/exports). If something can legitimately fail at that point, let it raise so the underlying defect is visible and fixed, rather than silently degrading behavior.