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:
- Fix the actual error
- 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:
tryexceptfinally
Let’s understand them step by step.
Example 1 — Division by Zero Error
a = 10
b = 0
c = a / bOutput
ZeroDivisionError: division by zeroExplanation
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:
ZeroDivisionErrorExample 2 — Index Error
cloud_envs = ["aws", "gcp", "azure"]print(cloud_envs[0])
Output
awsNow let’s try:
print(cloud_envs[3])Output
IndexError: list index out of rangeExplanation
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.pyNow 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 runExplanation
Here’s what happens:
- Python first checks the code inside
try - Since index
4does 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
azureSince 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 rangeIn 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 runWhat 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 runExplanation
- Since index
2exists, thetryblock runs successfully - The
exceptblock is skipped - The
finallyblock 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
- LinkedIn: https://www.linkedin.com/in/raees-yaqoob-qazi-ryqs/
- Medium Blog: https://medium.com/@raeesyaqubqazi
- Blogspot: https://brillertechnologies.blogspot.com/
- Facebook Page: https://web.facebook.com/profile.php?id=61553548371216
- YouTube Channel: https://www.youtube.com/@RaeesQ.
- TikTok: https://www.tiktok.com/@mrryqs?_t=ZS-8y7t0fQfJKu&_r=1
Comments
Post a Comment