# 任务异常从未被获取

- **ID:** `python/asyncio-task-exception-never-retrieved`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

某个 Task 抛出异常，但没有人 await 它或调用 task.exception()。默认事件循环异常处理器在垃圾回收时打印此警告。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8+ | active | — | — |
| 3.9+ | active | — | — |
| 3.10+ | active | — | — |
| 3.11+ | active | — | — |
| 3.12+ | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   task = asyncio.create_task(worker())
task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None)
await task
   ```
2. **** (93% 成功率)
   ```
   results = await asyncio.gather(*tasks, return_exceptions=True)
for r in results:
    if isinstance(r, Exception):
        log.error('task failed', exc_info=r)
   ```

## 无效尝试

- **** — It's logged via loop.call_exception_handler, not the warnings module; the underlying failure persists. (90% 失败率)
- **** — The exception happens asynchronously inside the task, not at creation time. (85% 失败率)
