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 Expecting Exceptions

expecting and raising exceptions

I am having a problem with these thing. My raising exception code I could not figure out. These two are very complex. For me.

5 Answers

First of all, it's always a good idea to include your code in your question.

Catching and raising exceptions are used when you know something might crash your program. If you have a function that can take invalid input (like the input function), you surround that code with the try function. Then, you use the catch function to prevent the exceptions you think might happen. If there were no exceptions, you use the else keyword to continue reading your code.

The raise function is used when you can catch an exception manually. Instead of using the try, you use other methods like if statements. You then use the keyword "raise" to catch that error in your error handling code. That code might reset some variables and ask the user for input again, so instead of re-writing all of that code you write it in your catch and use it as your exception protocol for the rest of the script.

Use 3 backticks (```) to wrap your code.

    # Code example

And basically you need to do 2 things in this challenge. Determine whether or not a string is less than 3 characters long, and if it is throw an exception.

You can get the length of a string using the len() function. So your code should look something like this:

    if len(variable) < 3:
        raise ValueError

That was actually meant for another thread, I clicked on yours by accident and deleted my comment as soon as I realised my mistake.

But if you want we can definitely handle this by e-mail instead of clogging up this thread.

def suggest(product_idea): return product_idea + "inator" raise ValueError

no I have everything else besides skype. christiianallen3@gmail.com.

yes email is great

def suggest(product_idea): return product_idea + "inator" raise ValueError I had this down before would never send without work. Sorry. I'm getting two errors.