Error Handlers in Python

From the Flask cheat sheet ยท Error Handling ยท verified Jul 2026

Error Handlers

Catch and handle application errors

python
# Quick Reference
@app.errorhandler(404)
def not_found(error):
    return jsonify({'error': 'Not found'}), 404

@app.errorhandler(Exception)
def handle_error(error):
    return jsonify({'error': str(error)}), 500
๐Ÿ’ก Custom exceptions provide better error context
๐Ÿ” Log errors for debugging in production
โšก Don't expose internal errors to users
๐Ÿ“Œ HTTPException covers all HTTP status codes
errorsexceptions
Back to the full Flask cheat sheet