Skip to content

Lesson 09 — Python Error Handling

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.


After completing this lesson, you will be able to:

  • Understand Python exceptions.
  • Handle errors using try and except.
  • Use else and finally.
  • Raise exceptions.
  • Create custom exceptions.
  • Log application errors.
  • Build reliable Python applications.

An Exception is an unexpected event that interrupts the normal execution of a program.

Example:

number = 10 / 0

Output:

ZeroDivisionError: division by zero

Instead of completing successfully, Python raises an exception.


Good error handling helps applications:

  • Prevent crashes
  • Improve user experience
  • Protect data
  • Continue processing
  • Simplify troubleshooting
  • Improve reliability

Enterprise software must always expect failures.


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

Program Starts
Code Executes
Exception Occurs
Exception Handler
Continue or Exit Safely

Place code that might fail inside a try block.

try:
result = 100 / 0

Python watches for exceptions while executing the code.


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.


try:
age = int(input("Enter Age: "))
except ValueError:
print("Please enter a valid number.")

This prevents invalid input from terminating the application.


try:
with open("config.json") as file:
print(file.read())
except FileNotFoundError:
print("Configuration file not found.")

Applications should always verify required files exist.


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.


try:
print(server)
except Exception as error:
print(error)

Output:

name 'server' is not defined

The variable error contains details about the exception.


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 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

Programs can intentionally generate exceptions.

age = -5
if age < 0:
raise ValueError("Age cannot be negative.")

Raising exceptions helps enforce business rules.


Create your own exception.

class InvalidUserError(Exception):
pass

Use it:

raise InvalidUserError("Unauthorized user.")

Custom exceptions improve code readability and troubleshooting.


Enterprise applications log exceptions for later analysis.

import logging
logging.basicConfig(level=logging.ERROR)
logging.error("Application Error")

Logs help administrators identify production issues.


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.


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.


Cloud Engineers handle:

  • API failures
  • Authentication problems
  • Network interruptions
  • Missing resources
  • Rate limits
  • Invalid cloud configurations

Robust error handling improves automation reliability.


DevOps Engineers handle:

  • Build failures
  • Deployment errors
  • Missing configuration files
  • Infrastructure provisioning failures
  • CI/CD pipeline interruptions

Reliable automation scripts should never fail silently.


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.")

AI developers commonly handle:

  • Missing datasets
  • Invalid model files
  • Memory limitations
  • API failures
  • Unsupported input formats

Exception handling improves application stability.


Avoid these beginner mistakes:

  • Ignoring exceptions.
  • Using only a generic except.
  • Forgetting the finally block.
  • Suppressing useful error messages.
  • Not validating user input.
  • Assuming APIs always succeed.

Testing failure scenarios is just as important as testing successful ones.


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.


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 except blocks.
  • Test failure scenarios.
  • Keep exception handling simple and readable.

Reliable applications are designed for both success and failure.


After completing this lesson, you should understand:

  • Python exceptions.
  • try.
  • except.
  • else.
  • finally.
  • Raising exceptions.
  • Custom exceptions.
  • Logging errors.
  • Exception handling best practices.

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.


➡️ 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.