This handles a specific exception type for bad requests.
Flask
from werkzeug.exceptions import BadRequest
@app.errorhandler(BadRequest)
def handle_bad_request(error):
return"Bad request!", 400
Sample Program
This Flask app has two error handlers: one for 404 errors and one for 500 errors. When a user visits a page that does not exist, they see a friendly message. If the server has an error, they see a different message.
Flask
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return"Welcome to the home page!"
@app.errorhandler(404)
def page_not_found(error):
return"Sorry, this page does not exist.", 404
@app.errorhandler(500)
def server_error(error):
return"Oops! Something went wrong on the server.", 500if __name__ == '__main__':
app.run(debug=True)
OutputSuccess
Important Notes
Use error handler decorators to keep your app user-friendly and stable.
Remember to return both a message and the correct HTTP status code.
Testing your error handlers by visiting wrong URLs or causing errors helps ensure they work.
Summary
Error handler decorators catch errors and let you respond nicely.
Use @app.errorhandler with error codes or exceptions.
Return a message and status code to inform users clearly.
Practice
(1/5)
1. What is the main purpose of using @app.errorhandler decorator in Flask?
easy
A. To catch specific errors and return custom responses
B. To define routes for the application
C. To start the Flask server
D. To connect to a database
Solution
Step 1: Understand the role of error handlers
Error handlers catch errors like 404 or 500 and let you respond with custom messages.
Step 2: Recognize the decorator's purpose
@app.errorhandler is used to register functions that handle specific errors gracefully.
Final Answer:
To catch specific errors and return custom responses -> Option A
Quick Check:
Error handler = Custom error response [OK]
Hint: Error handlers catch errors and customize replies [OK]
Common Mistakes:
Confusing errorhandler with route decorator
Thinking it starts the server
Assuming it manages database connections
2. Which of the following is the correct syntax to handle 404 errors in Flask?
easy
A. @app.errorhandler('404')
def handle_404(error):
return 'Not Found', 404
B. @app.errorhandler(404)
def handle_404(error):
return 'Not Found', 404
C. @app.route(404)
def handle_404():
return 'Not Found'
D. @app.errorhandler(HTTP_404)
def handle_404(error):
return 'Not Found', 404
Solution
Step 1: Identify correct decorator usage
The @app.errorhandler decorator takes an integer error code like 404, not a string or undefined constant.
Step 2: Check function signature and return
The handler function must accept an error argument and return a response with status code.
Final Answer:
@app.errorhandler(404)
def handle_404(error):
return 'Not Found', 404 -> Option B
Quick Check:
Use integer error code with errorhandler [OK]
Hint: Use integer error code, not string or constants [OK]
Common Mistakes:
Using @app.route instead of @app.errorhandler
Passing error code as string '404'
Using undefined constants like HTTP_404
3. What will be the output when a 500 error occurs with this code?