Exception Handling in Python — A DevOps Engineer’s Real-Life Perspective

Today I’m going to discuss Exception Handling in Python from a DevOps engineer’s point of view.

What is Exception Handling?

Before understanding exception handling, let’s first understand what an exception is.

An exception is an error that occurs during program execution. If the error is not handled properly, the program stops immediately.

As DevOps engineers, we deal with systems, automation, scripts, servers, cloud platforms, CI/CD pipelines, and monitoring tools daily. Because of this, errors and bugs are very common in our routine work.

Sometimes the issue is fixed by developers because they have deep knowledge of the application code. But from the DevOps side, we often need to handle the error properly so the automation or service does not completely fail.

This process is called Exception Handling.

Why Do We Use Exception Handling?

There are mainly two ways to deal with errors:

  1. Fix the actual error
  2. Handle the error gracefully using exception handling

In exception handling, we catch the error and prevent the application or script from crashing.

This helps:

  • Keep the program running
  • Avoid unexpected failures
  • Improve automation reliability
  • Handle real-world production scenarios

Python Exception Handling Keywords

Python mainly uses these three keywords:

  1. try
  2. except
  3. finally

Let’s understand them step by step.

Example 1 — Division by Zero Error

a = 10
b = 0
c = a / b

Output

ZeroDivisionError: division by zero

Explanation

In the above code, we are dividing 10 by 0.

Mathematically, division by zero results in infinity, but computers cannot process infinite values in this way. Therefore, Python throws an error called:

ZeroDivisionError

Example 2 — Index Error

cloud_envs = ["aws", "gcp", "azure"]
print(cloud_envs[0])

Output

aws

Now let’s try:

print(cloud_envs[3])

Output

IndexError: list index out of range

Explanation

Our list contains only 3 values:

  • Index 0 → aws
  • Index 1 → gcp
  • Index 2 → azure

There is no value at index 3, so Python throws an error.

Handling the Exception with Try and Except

Create a file named:

exception_handling.py

Now write this code:

cloud_envs = ["aws", "gcp", "azure"]
try:
print(cloud_envs[4])
except:
print("Exception handled")
print("This code should run")

Output

Exception handled
This code should run

Explanation

Here’s what happens:

  • Python first checks the code inside try
  • Since index 4 does not exist, an error occurs
  • Instead of stopping the program, Python moves to except
  • The exception is handled successfully
  • The remaining code continues to run

Now change:

print(cloud_envs[4])

to:

print(cloud_envs[2])

Output

azure

Since index 2 exists, no exception occurs.

What Happens Without Exception Handling?

cloud_envs = ["aws", "gcp", "azure"]
print(cloud_envs[4])
print("This code should run")

Output

IndexError: list index out of range

In this case:

  • The program stops immediately
  • The last print statement never executes

This is why exception handling is important in automation and production environments.

Understanding Finally in Python

Now let’s understand the role of finally.

The finally block always executes whether an exception occurs or not.

cloud_envs = ["aws", "gcp", "azure"]
try:
print(cloud_envs[4])
except:
print("Exception handled")
finally:
print("I will execute anyways")
print("This code should run")

Output

Exception handled
I will execute anyways
This code should run

What If No Error Occurs?

Now replace index 4 with 2.

cloud_envs = ["aws", "gcp", "azure"]
try:
print(cloud_envs[2])
except:
print("Exception handled")
finally:
print("I will execute anyways")
print("This code should run")

Output

azure
I will execute anyways
This code should run

Explanation

  • Since index 2 exists, the try block runs successfully
  • The except block is skipped
  • The finally block still executes
  • Remaining code also runs successfully

Final Thoughts

As DevOps engineers, we work heavily with:

  • Automation scripts
  • CI/CD pipelines
  • Cloud infrastructure
  • Monitoring systems
  • Production environments

Errors are normal in real-world systems. The important thing is not only fixing them but also handling them properly so services continue running smoothly.

Python exception handling helps us build more stable, reliable, and production-ready automation.

Happy Learning 🚀

🌐 Online References

Comments

Popular posts from this blog

📘 Understanding Prometheus in a Simple Way-Part 3 (For DevOps Beginners)

Grafana Setup & Dashboard Creation (Part-5)— Explained by Raees Yaqoob Qazi

My First Python Program: A Simple Calculator