0
0
Flaskframework~20 mins

Why error handling matters in Flask - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Flask Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do we use error handling in Flask apps?

What is the main reason to add error handling in a Flask web application?

ATo prevent users from accessing any page without login
BTo catch and respond to unexpected problems so the app doesn’t crash and users get helpful messages
CTo automatically fix bugs in the code without developer input
DTo make the app run faster by skipping error checks
Attempts:
2 left
πŸ’‘ Hint

Think about what happens if something goes wrong in your app and you don’t handle it.

❓ component_behavior
intermediate
2:00remaining
What happens when a Flask route raises an unhandled exception?

Consider this Flask route code:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 1 / 0  # division by zero error

What will the user see when visiting '/'?

AThe app automatically redirects to '/' again
BThe browser shows a blank page with no error
CA server error page with status code 500 is shown
DThe page shows '0' as the result
Attempts:
2 left
πŸ’‘ Hint

What does Flask do by default when an error happens inside a route?

πŸ“ Syntax
advanced
2:00remaining
Which Flask error handler syntax is correct?

Which option correctly defines a Flask error handler for 404 errors?

A
@app.errorhandler(404)
def not_found(error):
    return 'Page not found', 404
B
app.handle_error(404)
def not_found(error):
    return 'Page not found', 404
C
app.errorhandler(404)
def not_found(error):
    return 'Page not found', 404
D
@app.handle_error(404)
def not_found(error):
    return 'Page not found', 404
Attempts:
2 left
πŸ’‘ Hint

Remember how decorators are used in Flask to register error handlers.

❓ state_output
advanced
2:00remaining
What is the output when a Flask error handler returns a custom message?

Given this Flask app snippet:

from flask import Flask, abort
app = Flask(__name__)

@app.errorhandler(403)
def forbidden(e):
    return 'Access denied', 403

@app.route('/secret')
def secret():
    abort(403)

What will a user see when visiting '/secret'?

AThe page will redirect to '/'
BA default 403 error page from Flask
CA 404 Not Found error page
DThe text 'Access denied' with HTTP status 403
Attempts:
2 left
πŸ’‘ Hint

Check what the error handler returns and what status code it sets.

πŸ”§ Debug
expert
3:00remaining
Why does this Flask error handler not work as expected?

Look at this Flask code:

from flask import Flask
app = Flask(__name__)

@app.errorhandler(500)
def internal_error():
    return 'Server error', 500

@app.route('/')
def home():
    return 1 / 0

When visiting '/', the custom error message is NOT shown. Why?

AThe error handler function must accept an error parameter to work properly
BThe route '/' does not raise any error, so handler is not called
CThe error handler decorator is misspelled and not recognized
DFlask does not support custom handlers for 500 errors
Attempts:
2 left
πŸ’‘ Hint

Check the function signature of the error handler.