Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Functions and Looping Raise an Exception

Afshin Taheri
Afshin Taheri
627 Points

I really dont understand what does Raise do??

I think this task does not make sense at all. what is diference between Raise and except

suggestinator.py
def suggest(product_idea):
    if len(product_idea) > 3:
        return product_idea + "inator"
    else:    
        raise ValuError

2 Answers

Steven Parker
Steven Parker
231,275 Points

The main difference is that "raise" is the instruction that creates an exception event, and "except" is the instruction that defines where the program flow will jump to when an exception occurs.

If you consider the exception to be like a baseball, then "raise" would be the pitcher, and "except" would be the catcher.

Does that make it more clear?

Schaffer Robichaux
Schaffer Robichaux
21,729 Points

Hi Afshin,

I often refer to this stackoverflow question in which the most up-voted response does an excellent job of showing you how to pick the best exception handler constructor that semantically fits your use case- as well as comparing them.