# 停止迭代：

- **ID:** `python/stopiteration-next-on-empty-generator`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在没有默认值的情况下对没有更多项的生成器或迭代器调用 next()。

## 版本兼容性

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

## 解决方案

1. **Provide default value to next()** (95% 成功率)
   ```
   item = next(generator, None)
   ```
2. **Use for loop instead of next()** (90% 成功率)
   ```
   for item in generator:
    process(item)
   ```

## 无效尝试

- **Catching StopIteration with a bare except** — Bare except catches all exceptions, potentially hiding other bugs. (60% 失败率)
- **Using list(iterator) without checking length** — list() consumes the iterator but does not handle empty case gracefully. (40% 失败率)
