0
0
Flaskframework~10 mins

API error handling in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - API error handling
Client sends request
API receives request
Process request
Error occurs?
NoReturn success response
Yes
Catch error
Return error response with status code
Client receives error info
The API receives a request, processes it, and if an error happens, it catches it and sends back an error response with a status code.
Execution Sample
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/divide/<int:a>/<int:b>')
def divide(a, b):
    try:
        result = a / b
        return jsonify({'result': result})
    except ZeroDivisionError:
        return jsonify({'error': 'Cannot divide by zero'}), 400
This API endpoint divides two numbers and returns the result or an error if dividing by zero.
Execution Table
StepInput (a,b)ActionResultResponse Sent
1a=10, b=2Try division10 / 2 = 5.0{'result': 5.0}, status 200
2a=5, b=0Try divisionZeroDivisionError raised{'error': 'Cannot divide by zero'}, status 400
3a=7, b=1Try division7 / 1 = 7.0{'result': 7.0}, status 200
4a=3, b=0Try divisionZeroDivisionError raised{'error': 'Cannot divide by zero'}, status 400
5-No more requests--
💡 Execution stops after all requests are processed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
a-10573-
b-2010-
result-5.0Error7.0Error-
response-{'result': 5.0}, 200{'error': 'Cannot divide by zero'}, 400{'result': 7.0}, 200{'error': 'Cannot divide by zero'}, 400-
Key Moments - 2 Insights
Why does the API return a 400 status code when dividing by zero?
Because the ZeroDivisionError is caught and the API sends a JSON error message with status 400, as shown in execution_table rows 2 and 4.
What happens if no error occurs during division?
The API returns the division result in JSON with status 200, as seen in execution_table rows 1 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response when a=5 and b=0?
A{'result': 1.0}, status 200
B{'result': 5.0}, status 200
C{'error': 'Cannot divide by zero'}, status 400
DNo response
💡 Hint
Check row 2 in the execution_table where b=0 causes an error response.
At which step does the division succeed without error?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look for steps where 'result' is a number, not 'Error' in variable_tracker.
If we remove the try-except block, what would happen when b=0?
AAPI crashes and returns a server error (500)
BAPI returns a JSON error with status 400
CAPI returns result 0
DAPI returns status 200 with empty response
💡 Hint
Without catching ZeroDivisionError, Python raises an exception causing a server error.
Concept Snapshot
API error handling in Flask:
- Use try-except inside route functions
- Catch specific errors (e.g., ZeroDivisionError)
- Return JSON error message with HTTP status code
- Normal responses return data with status 200
- Helps client understand what went wrong
Full Transcript
This example shows how a Flask API handles errors by using try-except blocks inside route functions. When dividing two numbers, if the divisor is zero, a ZeroDivisionError occurs. The API catches this error and returns a JSON response with an error message and status code 400. If no error occurs, it returns the division result with status 200. This approach helps clients know if their request failed and why, improving communication and reliability.