Python’s Raise Keyword

How to manually throw an exception in Python

Photo by Jonathan Daniels on Unsplash

Exception handling in Python can be daunting. I find it particularly difficult because as a researcher, I’m just not very good at thinking like a ‘programmer’ should. I’m thinking more about the speed of my optimisation procedures, rather than ‘is my code handling all edge cases’.

For better or worse, that’s my flaw in coding. However over time, I’ve picked up a few tricks that’ve helped in making life easier in terms of making more robust coding.

Keywords in Python make life a lot easier and I’ve previously been over Ternary Conditional Operators and the keyword Yield. Let’s cover then the keyword ‘raise’.

The keyword raise is used when the coder wants to bring forward an exception conditional on something occurring. As an example, the syntax is as follows:

if test:
raise Exception(Message)

Given this, we could use it as follows:

string = "Ciao"

if string=="Ciao" or string=="Howdy" or string=="Bye":
raise Exception("This word is not allowed")

Exception: This word is not allowed

As you can see here — if the condition passes (in this case, the string does indeed say ‘Hello’, then the exception is raised. Likewise:

# Input a positive number and raise an exception 
# if input is a negative value

num = int(input("Enter a positive number: "))

if num<0:
raise Exception("Please input only positive value ")

print("num = ", num)

which will give the result:

First run:
Enter a positive number: 20
num = 20

Second run:
Enter a positive number: -10
Traceback (most recent call last):
File "/home/", line 10, in <module>
raise Exception("Please input only positive value ")
Exception: Please input only positive value

So as you can see, once the user inputs a number that is not a positive integer, the specific exception that we defined has been raised. This is great because now, we have control over a specific common problem that can arise, giving us more control over our code.

The keyword raise is super easy to use. All you need to do is be clear on what you’re trying to test for and what you want to do once you’ve found it.

Python has been made with simplicity in mind and the keyword raise makes it easier for coders to be able to control their handling of exceptions.

Using this method has made my work a lot better defined and in particular, has meant that I’ve had more control over my code. This really helps when you look back on code after a couple of months.

Thanks for reading! If you have any messages, please let me know!

Keep up to date with my latest articles here!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Website Powered by

Up ↑

%d bloggers like this: