# starlette.exceptions.HTTPException: 404：未找到。

- **ID:** `python/starlette-http-exception-status-code`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

某个路由或依赖显式抛出了 HTTPException(404)，或路由器未匹配到任何路径，且异常未被自定义处理器捕获。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 0.37.x | active | — | — |
| 0.40.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   @app.exception_handler(HTTPException)
async def handler(request, exc):
    return JSONResponse({'detail': exc.detail}, status_code=exc.status_code)
   ```
2. **** (90% 成功率)
   ```
   Register the handler on the specific router or app instance used in tests, not on a different app object.
   ```

## 无效尝试

- **** — HTTPException is handled by Starlette's HTTPException handler before generic Exception; order matters and generic handler may not fire. (80% 失败率)
- **** — Duplicates logic across every route and bypasses OpenAPI error docs. (70% 失败率)
