What is the purpose of error handler decorators in Flask?
Error handler decorators in Flask let you define functions that run automatically when specific errors happen. They help you show friendly messages or handle problems smoothly.
Click to reveal answer
beginner
How do you use the @app.errorhandler decorator in Flask?
You put @app.errorhandler(error_code) above a function. This function will run when that error happens. For example, @app.errorhandler(404) runs when a page is not found.
Click to reveal answer
intermediate
What does the function decorated with @app.errorhandler receive as input?
The function receives the error object that caused the problem. You can use it to get details about the error and decide what to show the user.
Click to reveal answer
intermediate
Can you handle multiple error codes with one error handler decorator in Flask?
No, you cannot handle multiple error codes with one @app.errorhandler by passing a tuple like (404, 500). You need to define separate functions for each error code or use base exception classes for broader coverage.
Click to reveal answer
intermediate
What is the benefit of using error handler decorators instead of try-except blocks in Flask routes?
Error handler decorators keep your route code clean and separate error handling logic. They automatically catch errors app-wide, so you don't repeat try-except in every route.
Click to reveal answer
What does @app.errorhandler(404) do in a Flask app?
ADefines a new route
BRuns a function when a page is not found
CHandles user login
DRuns a function when the server starts
✗ Incorrect
The decorator @app.errorhandler(404) runs the decorated function when a 404 Not Found error occurs.
What argument does the function decorated with @app.errorhandler receive?
AThe request object
BNo arguments
CThe error object
DThe response object
✗ Incorrect
The error handler function receives the error object that triggered the error.
Can one @app.errorhandler decorator handle multiple error codes?
ANo, it handles one error code or exception type
BYes, it can handle any number of errors
COnly if errors are related
DOnly for HTTP errors
✗ Incorrect
@app.errorhandler handles one error code or exception type per decorated function. To handle multiple errors, define multiple handlers.
Why use error handler decorators instead of try-except in routes?
ATo avoid writing functions
BBecause try-except is not allowed in Flask
CTo speed up the app
DTo keep routes clean and handle errors app-wide
✗ Incorrect
Error handler decorators separate error logic and apply globally, keeping routes simpler.
Which of these is a correct way to define a 500 error handler in Flask?