B. Incorrect decorator name; should be @app.handle_error.
C. Return statement should not include status code.
D. Missing error parameter in the handler function.
Solution
Step 1: Check error handler function signature
Flask error handlers must accept an error argument to receive error info.
Step 2: Verify decorator correctness
The decorator @app.errorhandler(500) is correct; function name can be anything.
Final Answer:
Missing error parameter in the handler function. -> Option D
Quick Check:
Error handler needs error param = Missing error parameter in the handler function. [OK]
Hint: Error handlers need an error argument [OK]
Common Mistakes:
Omitting the error parameter in handler function
Using wrong decorator names
Thinking function name is fixed
5. You want to show a custom message for both 404 and 500 errors in your Flask app. Which approach correctly handles both errors without repeating code?
hard
A. Define two separate handlers with identical code for 404 and 500 errors.
B. Use a single function and register it twice with @app.errorhandler(404) and @app.errorhandler(500).
C. Use @app.route for 404 and @app.errorhandler for 500 errors.
D. Handle errors only in the main route function with try-except blocks.
Solution
Step 1: Understand code reuse for error handlers
To avoid repeating code, one function can handle multiple error codes by registering it multiple times.
Step 2: Check Flask syntax for multiple error handlers
Flask allows decorating the same function multiple times with different error codes.
Final Answer:
Use a single function and register it twice with @app.errorhandler(404) and @app.errorhandler(500). -> Option B
Quick Check:
One function, multiple decorators = Use a single function and register it twice with @app.errorhandler(404) and @app.errorhandler(500). [OK]
Hint: Register one function with multiple @app.errorhandler decorators [OK]
Common Mistakes:
Writing duplicate code in separate handlers
Using @app.route for error handling
Trying to catch errors only inside routes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.