---
source: https://iinventedlight.com/library/notes/fastapi-exception-handlers.md
title: "FastAPI HTTP errors and exception handlers"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (FastAPI docs); MIT (FastAPI source docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Handling Errors; FastAPI source docs, handling-errors.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: fastapi exceptions"
tags: [fastapi, httpexception, errors, handlers]
client: Librarian
sources:
  - id: S1
    title: "Handling Errors"
    publisher: FastAPI docs
    url: https://fastapi.tiangolo.com/tutorial/handling-errors/
    accessed: 2026-11-18
    license: "MIT"
  - id: S2
    title: "handling-errors.md"
    publisher: FastAPI source docs
    url: https://raw.githubusercontent.com/fastapi/fastapi/master/docs/en/docs/tutorial/handling-errors.md
    accessed: 2026-11-18
    license: "MIT"
---
# FastAPI HTTP errors and exception handlers
**Answer:** You may need to report an error to a client that is using your API. Raise `HTTPException` with a status code. You can also add an exception handler.

## Examples

```python
from fastapi import FastAPI, HTTPException

app = FastAPI()

@app.get("/items/{item_id}")
def read_item(item_id: str):
    if item_id != "known":
        raise HTTPException(status_code=404, detail="Item not found")
    return {"id": item_id}
```

## Rules


## See also

- [fastapi-dependencies](https://iinventedlight.com/library/notes/fastapi-dependencies/)

## Claims

- id: C1
  claim: APIs often need to report an error to a client.
  source: S1
  evidence: "report an error to a client"

- id: C2
  claim: HTTPException is the documented error type.
  source: S1
  evidence: "HTTPException"

- id: C3
  claim: The source docs describe reporting an error to a client.
  source: S2
  evidence: "report an error to a client"

## Sources

- [S1] Handling Errors. FastAPI docs. https://fastapi.tiangolo.com/tutorial/handling-errors/ . License: MIT. Accessed 2026-11-18.

- [S2] handling-errors.md. FastAPI source docs. https://raw.githubusercontent.com/fastapi/fastapi/master/docs/en/docs/tutorial/handling-errors.md . License: MIT. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Handling Errors; FastAPI source docs, handling-errors.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
