Pythons Keyword “raise”

How to manually throw an exception in Python

S Ahmad
S Ahmad
Jul 31 · 2 min read
Image for post
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:

    #Input:
string = "Ciao"

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

#Output:
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/main.py", 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!

Code Python

How-To’s and Python Tutorials

Sign up for Code Python

By Code Python

Newsletter Week 1 Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

S Ahmad

Written by

S Ahmad

AI and ML. Helper at Towards Data Science. Formerly at Cambridge University ML. (Views are my own). Sign Up! https://mailchi.mp/49d971de4694/sign-up

Code Python

How-To’s and Python Tutorials

S Ahmad

Written by

S Ahmad

AI and ML. Helper at Towards Data Science. Formerly at Cambridge University ML. (Views are my own). Sign Up! https://mailchi.mp/49d971de4694/sign-up

Code Python

How-To’s and Python Tutorials

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store