📖 Scenario: You are building a simple Flask web application that shows a welcome message. You want to make sure that if a user visits a page that does not exist, the app shows a friendly error page instead of a default error message.
🎯 Goal: Create a Flask app with a route for the home page. Then add error handling for 404 errors to show a custom message. This helps users understand when they visit a wrong page.
📋 What You'll Learn
Create a Flask app instance named app
Add a route for / that returns the text 'Welcome to the homepage!'
Create an error handler for 404 errors using @app.errorhandler(404)
The 404 error handler should return the text 'Sorry, page not found.' and the status code 404
💡 Why This Matters
🌍 Real World
Web applications need to handle errors gracefully to improve user experience and avoid showing confusing default error pages.
💼 Career
Knowing how to add error handling in Flask is essential for backend developers to build reliable and user-friendly web services.
Progress0 / 4 steps
1
Set up the Flask app and home route
Import Flask from flask. Create a Flask app instance called app. Add a route for / that returns the text 'Welcome to the homepage!'.
Flask
Hint
Remember to import Flask and create the app instance before adding routes.
2
Add error handler for 404 errors
Add an error handler for 404 errors using @app.errorhandler(404). Define a function called page_not_found that takes an error parameter.
Flask
Hint
Use the @app.errorhandler(404) decorator to catch 404 errors.
3
Return custom message and status code in 404 handler
In the page_not_found function, return the text 'Sorry, page not found.' and the status code 404 as a tuple.
Flask
Hint
Return a tuple with the message and the status code 404.
4
Add the app run block for production
Add the block if __name__ == '__main__': and inside it call app.run() to start the Flask app.
Flask
Hint
This block makes sure the app runs when you start the script directly.
Practice
(1/5)
1. What is the main purpose of using @app.errorhandler in a Flask application?
easy
A. To catch specific errors and provide custom responses
B. To speed up the application startup
C. To automatically restart the server on errors
D. To log user activities
Solution
Step 1: Understand the role of @app.errorhandler
This decorator is used to catch specific HTTP or application errors in Flask.
Step 2: Recognize its use for custom responses
It allows developers to return user-friendly error pages or messages instead of default error traces.
Final Answer:
To catch specific errors and provide custom responses -> Option A
Quick Check:
Error handling = custom responses [OK]
Hint: Errorhandler decorates functions to catch and respond to errors [OK]
Common Mistakes:
Thinking it restarts the server automatically
Confusing errorhandler with logging functions
Assuming it speeds up app startup
2. Which of the following is the correct syntax to handle 404 errors in Flask?
easy
A. @app.error(404)
def not_found(error):
return 'Not found', 404
B. @app.errorhandler(404)
def not_found(error):
return 'Page not found', 404
C. @app.handle_error(404)
def not_found():
return 'Page missing', 404
D. @app.catch_error(404)
def not_found(error):
return '404 error', 404
Solution
Step 1: Identify the correct decorator name
The correct Flask decorator for error handling is @app.errorhandler.
Step 2: Confirm function signature and return
The function should accept an error parameter and return a response with the error code.
Final Answer:
@app.errorhandler(404)\ndef not_found(error):\n return 'Page not found', 404 -> Option B
Quick Check:
Correct decorator and function signature = @app.errorhandler(404)
def not_found(error):
return 'Page not found', 404 [OK]
Hint: Use @app.errorhandler with error code and error param [OK]
Common Mistakes:
Using wrong decorator names like @app.handle_error
Omitting the error parameter in the function
Returning without the status code
3. Given this Flask code snippet, what will the user see if a 500 error occurs?