Consider this Flask route that raises a ValueError which is caught by a custom error handler. What will the client receive as a response?
from flask import Flask, jsonify app = Flask(__name__) @app.route('/test') def test_route(): raise ValueError('Invalid value') @app.errorhandler(ValueError) def handle_value_error(e): return jsonify({'error': str(e)}), 400 # Assume app.run() is called elsewhere
Think about what happens when an exception is raised and a matching error handler exists.
The ValueError raised in the route is caught by the @app.errorhandler(ValueError) decorated function. This handler returns a JSON response with the error message and a 400 status code. So the client receives the JSON error message with status 400.
In Flask, you want to create a custom response for 404 Not Found errors. Which code snippet correctly registers this handler?
Check the type of argument @app.errorhandler expects for HTTP status codes.
The @app.errorhandler decorator accepts an integer HTTP status code like 404 to register a handler for that error. Option B uses 404 as an integer, which is correct. Option B uses a string '404' which is invalid. Option B registers a route, not an error handler. Option B uses a generic exception class, which is not specific to 404.
Given this Flask app code, the custom exception MyError raised in the route is caught by the error handler. Why?
from flask import Flask app = Flask(__name__) class MyError(Exception): pass @app.route('/fail') def fail(): raise MyError('Oops') @app.errorhandler(Exception) def handle_exception(e): return 'Handled error', 500 # app.run() elsewhere
Consider Python inheritance and Flask's error handling behavior.
Since MyError inherits from Exception, and the error handler is registered for Exception, it will catch MyError. The order of registration does not affect catching. Flask catches all exceptions raised in routes and passes them to matching handlers. So option A is correct.
In this Flask app, the route raises a KeyError which has no registered error handler. What status code does the client receive?
from flask import Flask app = Flask(__name__) @app.route('/key') def key_route(): raise KeyError('missing key') # No error handler for KeyError # app.run() elsewhere
Think about Flask's default behavior when exceptions are not handled.
When a route raises an exception that is not caught by any error handler, Flask returns a 500 Internal Server Error response by default. So the client receives status code 500.
Flask has special handling for exceptions derived from werkzeug.exceptions.HTTPException. Which statement best describes this behavior?
Recall how Flask uses HTTPException to create HTTP error responses.
Flask treats exceptions derived from HTTPException specially by automatically creating HTTP responses using their status code and description. This means if you raise a 404 Not Found exception, Flask sends a 404 response unless you override it with a custom handler. Other exceptions default to 500 errors unless handled.