Lesson 09 — Python Error Handling
Lesson 09 — Python Error Handling
Section titled “Lesson 09 — Python Error Handling”Lesson Overview
Section titled “Lesson Overview”No software is perfect.
Whether you’re developing:
- Cloud automation scripts
- REST API integrations
- AI applications
- Security tools
- DevOps pipelines
…unexpected problems will always occur.
For example:
- A file may not exist.
- A cloud API may be unavailable.
- A user may enter invalid input.
- A network connection may fail.
- A database server may become unreachable.
- Authentication may fail.
Without proper error handling, your application will crash.
Professional software is designed to anticipate failures and recover gracefully.
Python provides Exception Handling, allowing developers to detect errors, display meaningful messages, recover when possible, and prevent applications from terminating unexpectedly.
Learning Objectives
Section titled “Learning Objectives”After completing this lesson, you will be able to:
- Understand Python exceptions.
- Handle errors using
tryandexcept. - Use
elseandfinally. - Raise exceptions.
- Create custom exceptions.
- Log application errors.
- Build reliable Python applications.
What is an Exception?
Section titled “What is an Exception?”An Exception is an unexpected event that interrupts the normal execution of a program.
Example:
number = 10 / 0Output:
ZeroDivisionError: division by zeroInstead of completing successfully, Python raises an exception.
Why Error Handling Matters
Section titled “Why Error Handling Matters”Good error handling helps applications:
- Prevent crashes
- Improve user experience
- Protect data
- Continue processing
- Simplify troubleshooting
- Improve reliability
Enterprise software must always expect failures.
Common Exceptions
Section titled “Common Exceptions”| Exception | Cause |
|---|---|
| ZeroDivisionError | Divide by zero |
| FileNotFoundError | Missing file |
| ValueError | Invalid value |
| TypeError | Wrong data type |
| IndexError | Invalid list index |
| KeyError | Missing dictionary key |
| NameError | Undefined variable |
| ModuleNotFoundError | Module missing |
| PermissionError | Access denied |
Exception Flow
Section titled “Exception Flow”Program Starts
↓
Code Executes
↓
Exception Occurs
↓
Exception Handler
↓
Continue or Exit SafelyThe try Block
Section titled “The try Block”Place code that might fail inside a try block.
try: result = 100 / 0Python watches for exceptions while executing the code.
The except Block
Section titled “The except Block”Handle the exception.
try: result = 100 / 0
except ZeroDivisionError: print("Cannot divide by zero.")Output:
Cannot divide by zero.Instead of crashing, the program displays a useful message.
Handling User Input
Section titled “Handling User Input”try:
age = int(input("Enter Age: "))
except ValueError:
print("Please enter a valid number.")This prevents invalid input from terminating the application.
Handling Missing Files
Section titled “Handling Missing Files”try:
with open("config.json") as file: print(file.read())
except FileNotFoundError:
print("Configuration file not found.")Applications should always verify required files exist.
Handling Multiple Exceptions
Section titled “Handling Multiple Exceptions”try:
value = int(input())
result = 100 / value
except ValueError:
print("Invalid number.")
except ZeroDivisionError:
print("Division by zero is not allowed.")Each exception type can have its own handler.
Generic Exception Handling
Section titled “Generic Exception Handling”try:
print(server)
except Exception as error:
print(error)Output:
name 'server' is not definedThe variable error contains details about the exception.
The else Block
Section titled “The else Block”The else block executes only when no exception occurs.
try:
number = int(input())
except ValueError:
print("Invalid input.")
else:
print("Valid number entered.")Use else for code that should run only after successful execution.
The finally Block
Section titled “The finally Block”The finally block always executes.
try:
file = open("report.txt")
except FileNotFoundError:
print("Missing file.")
finally:
print("Closing program.")Typical uses:
- Closing files
- Closing database connections
- Releasing resources
- Cleaning temporary files
Raising Exceptions
Section titled “Raising Exceptions”Programs can intentionally generate exceptions.
age = -5
if age < 0:
raise ValueError("Age cannot be negative.")Raising exceptions helps enforce business rules.
Custom Exceptions
Section titled “Custom Exceptions”Create your own exception.
class InvalidUserError(Exception): passUse it:
raise InvalidUserError("Unauthorized user.")Custom exceptions improve code readability and troubleshooting.
Logging Errors
Section titled “Logging Errors”Enterprise applications log exceptions for later analysis.
import logging
logging.basicConfig(level=logging.ERROR)
logging.error("Application Error")Logs help administrators identify production issues.
Error Handling with APIs
Section titled “Error Handling with APIs”import requests
try:
response = requests.get(API_URL)
response.raise_for_status()
except requests.exceptions.RequestException:
print("Unable to reach the API.")Network failures should always be handled gracefully.
Error Handling with JSON
Section titled “Error Handling with JSON”import json
try:
with open("config.json") as file:
config = json.load(file)
except json.JSONDecodeError:
print("Invalid JSON file.")Applications should validate configuration files before using them.
Error Handling in Cloud Computing
Section titled “Error Handling in Cloud Computing”Cloud Engineers handle:
- API failures
- Authentication problems
- Network interruptions
- Missing resources
- Rate limits
- Invalid cloud configurations
Robust error handling improves automation reliability.
Error Handling in DevOps
Section titled “Error Handling in DevOps”DevOps Engineers handle:
- Build failures
- Deployment errors
- Missing configuration files
- Infrastructure provisioning failures
- CI/CD pipeline interruptions
Reliable automation scripts should never fail silently.
Error Handling in Cybersecurity
Section titled “Error Handling in Cybersecurity”Security professionals handle:
- Corrupted log files
- Missing evidence
- API authentication failures
- Network timeouts
- Invalid indicators of compromise (IOCs)
Example:
try:
with open("security.log") as file:
print(file.read())
except FileNotFoundError:
print("Security log unavailable.")Error Handling in AI
Section titled “Error Handling in AI”AI developers commonly handle:
- Missing datasets
- Invalid model files
- Memory limitations
- API failures
- Unsupported input formats
Exception handling improves application stability.
Common Mistakes
Section titled “Common Mistakes”Avoid these beginner mistakes:
- Ignoring exceptions.
- Using only a generic
except. - Forgetting the
finallyblock. - Suppressing useful error messages.
- Not validating user input.
- Assuming APIs always succeed.
Testing failure scenarios is just as important as testing successful ones.
Real-World Example
Section titled “Real-World Example”A Cloud Engineer creates a deployment script.
try:
with open("aws_config.json") as file:
print("Configuration Loaded")
except FileNotFoundError:
print("Configuration file missing.")
finally:
print("Deployment Complete.")Even if the configuration file is unavailable, the script exits cleanly and provides a meaningful message.
Best Practices
Section titled “Best Practices”As a Python developer:
- Handle specific exceptions whenever possible.
- Use meaningful error messages.
- Validate user input.
- Log errors for troubleshooting.
- Always clean up resources.
- Avoid empty
exceptblocks. - Test failure scenarios.
- Keep exception handling simple and readable.
Reliable applications are designed for both success and failure.
Key Takeaways
Section titled “Key Takeaways”After completing this lesson, you should understand:
- Python exceptions.
try.except.else.finally.- Raising exceptions.
- Custom exceptions.
- Logging errors.
- Exception handling best practices.
Summary
Section titled “Summary”Python Exception Handling enables applications to detect, manage, and recover from unexpected runtime errors.
By using try, except, else, finally, and custom exceptions, you can build reliable cloud automation, DevOps tools, AI applications, and cybersecurity scripts that continue operating even when problems occur.
Exception handling is a fundamental skill for writing production-ready Python software.
Next Lesson
Section titled “Next Lesson”➡️ Lesson 10 — Python Automation
In the next lesson, you’ll learn how to automate repetitive tasks using Python. You’ll build scripts that work with files, folders, cloud resources, APIs, system commands, and scheduled jobs—helping you become more productive in Cloud Computing, DevOps, AI, and Cybersecurity.