# AssertionError: Middleware order error: ExceptionMiddleware must be last

- **ID:** `python/starlette-http-middleware-order`
- **Domain:** python
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Adding middleware after ExceptionMiddleware in Starlette, breaking exception handling.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **** (90% success)
   ```
   Add middleware before adding routes: app = Starlette()
app.add_middleware(MyMiddleware)
# Then add routes
   ```
2. **** (85% success)
   ```
   If using add_middleware, ensure it's called before app initialization completes
   ```

## Dead Ends

- **** — Must follow specific order; ExceptionMiddleware should be innermost. (80% fail)
- **** — Loses built-in exception handlers. (60% fail)
