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

Don't understand how to use raise/except.

What exactly is the difference between raise and except? When do I use them? Attached my code for the challenge.

suggestinator.py
def suggest(product_idea):
    return product_idea + "inator"

try:
    product_idea = input("What's the name of your product?  ")
    if len(product_idea) < int(3):
        raise ValueError ("Oops! The name needs to be 4 or more characters long")
        except ValueError as err:
            print("Name not long enough. Try again...")

1 Answer

Steven Parker
Steven Parker
230,274 Points

The "raise" statement creates an exception, but the "except" statement indicates the code to be used to handle one when it occurs.

In this challenge, you'll only need to "raise" an exception, you won't need any "try" or "except" or other code to go with them. But remember to add your new code before the "return". Once that statement is encountered, the function ends.

And the number "3" is an "int" already, it doesn't need to be converted.

I can't believe it was that simple! Thank you Steven. A valuable lesson learned in being not overanalyzing the instructions.