python runtime_error ai_generated true

asyncio异常:任务异常从未被检索

asyncio.exceptions.CancelledError: Task exception was never retrieved

ID: python/asyncio-task-exception-silent

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2025-02-14首次发现

版本兼容性

版本状态引入弃用备注
3.7 active
3.8 active
3.9 active

根因分析

任务引发异常,但异常从未被等待或检索,导致静默失败。

English

A task raises an exception but the exception is never awaited or retrieved, leading to silent failures.

generic

解决方案

  1. 90% 成功率
    task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None)
  2. 95% 成功率
    try: await task except Exception as e: print(e)

无效尝试

常见但无效的做法:

  1. 90% 失败

    The exception is lost, making debugging difficult.

  2. 60% 失败

    If the task is not done, it may block or raise.