0
0
Flaskframework~20 mins

Exception handling in routes in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Exception Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a route raises a handled exception?

Consider this Flask route that raises a ValueError which is caught by a custom error handler. What will the client receive as a response?

Flask
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
ADefault Flask 500 Internal Server Error page
BEmpty response with status code 200
C{"error": "Invalid value"} with status code 400
DFlask debug traceback page
Attempts:
2 left
💡 Hint

Think about what happens when an exception is raised and a matching error handler exists.

📝 Syntax
intermediate
2:00remaining
Which option correctly registers an error handler for HTTP 404 errors?

In Flask, you want to create a custom response for 404 Not Found errors. Which code snippet correctly registers this handler?

A
@app.route('/404')
def not_found():
    return 'Page not found', 404
B
@app.errorhandler(404)
def not_found(e):
    return 'Page not found', 404
C
@app.errorhandler('404')
def not_found(e):
    return 'Page not found', 404
D
@app.errorhandler(HTTPException)
def not_found(e):
    return 'Page not found', 404
Attempts:
2 left
💡 Hint

Check the type of argument @app.errorhandler expects for HTTP status codes.

🔧 Debug
advanced
2:00remaining
Why does this Flask app catch the custom exception in the route?

Given this Flask app code, the custom exception MyError raised in the route is caught by the error handler. Why?

Flask
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
AThe error handler catches all exceptions, so it should catch MyError
BThe custom exception is not derived from Exception, so it is not caught
CThe error handler is registered after the route, so it is ignored
DFlask only catches HTTPException subclasses by default, so custom exceptions need explicit handling
Attempts:
2 left
💡 Hint

Consider Python inheritance and Flask's error handling behavior.

state_output
advanced
2:00remaining
What is the response status code when a route raises an unhandled exception?

In this Flask app, the route raises a KeyError which has no registered error handler. What status code does the client receive?

Flask
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
A500 Internal Server Error
B404 Not Found
C200 OK with empty body
D400 Bad Request
Attempts:
2 left
💡 Hint

Think about Flask's default behavior when exceptions are not handled.

🧠 Conceptual
expert
3:00remaining
How does Flask differentiate between HTTPException and other exceptions in error handling?

Flask has special handling for exceptions derived from werkzeug.exceptions.HTTPException. Which statement best describes this behavior?

AFlask converts all exceptions to HTTPException before handling
BHTTPException subclasses are ignored by Flask and always cause 500 errors
COnly exceptions not derived from HTTPException trigger error handlers
DHTTPException subclasses automatically generate HTTP responses with their code and description unless overridden
Attempts:
2 left
💡 Hint

Recall how Flask uses HTTPException to create HTTP error responses.