Understanding raise Exception in Python – A Beginner-Friendly Guide

 Today, I’m going to discuss a very important concept in Python: How to raise an Exception in code and why we use it.

As DevOps engineers, we often write automation scripts, CI/CD pipelines, and cloud management tools. In real-world projects, sometimes we want our program to stop execution when something unexpected happens. This is where raise Exception becomes useful.


What is raise Exception?

The raise keyword is used to manually generate an exception. When Python encounters a raised exception that is not handled, it immediately stops the program.

Example 1: Raising an Exception Outside a Try Block

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")
raise Exception("This is a new Exception")

Output

Exception handled
I will execute anyways
This code should run
Exception: This is a new Exception

Now let’s move the raise Exception before the last print statement:

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

What Happens?

The program will stop immediately after raise Exception.

Output:

Exception handled
I will execute anyways
Exception: This is a new Exception

The line:

print("This code should run")

will never execute because the exception stops the program.

Example 2: Raising an Exception Inside a Try Block

If we raise an exception inside the try block, Python can handle it using the except block.

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

Output

aws
Exception handled
I will execute anyways
This code should run

Since the exception is raised inside the try block, it is handled by except, and the program continues normally.

Handling Specific Exceptions

Python allows us to handle different types of exceptions separately.

cloud_envs = ["aws", "gcp", "azure"]
try:
print(cloud_envs[200])
    a = 10
b = 0
c = a / b
except ZeroDivisionError as e:
print("1:", e)
except IndexError as e:
print("2:", e)
finally:
print("I will execute anyways")

Why is this useful?

Python automatically identifies which exception occurred and jumps to the matching except block.

For example:

  • Accessing an invalid list index triggers IndexError.
  • Dividing a number by zero triggers ZeroDivisionError.

This is one of the reasons Python’s exception handling is powerful and easy to use.

How Can We See All Built-in Python Exceptions?

Python provides many built-in exceptions. To view them, use:

import builtins
print(dir(builtins))

This command displays all built-in functions, classes, and exception types available in Python.

Real-World DevOps Use Case

Imagine a user clicks an invalid URL on a web application, or an automation script receives incorrect input. Instead of allowing the application to continue with invalid data, we can raise an exception and stop execution immediately.

This helps us:

  • Prevent unexpected behavior
  • Avoid incorrect deployments
  • Improve debugging
  • Build more reliable automation scripts

Conclusion

raise Exception is a powerful feature in Python that allows developers to manually generate errors when something goes wrong. If the exception is not handled, the program stops immediately. If it is handled inside a try-except block, the application can recover gracefully and continue execution.

Understanding exception handling is an essential skill for every DevOps engineer because reliable automation depends on proper error handling.

I hope you found this blog helpful. Feel free to share it with others and follow for more DevOps and Python learning content.

🌐 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