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

exceptions and raising ValueErrors

are there any ways to determine when one is supposed to use the 'expcept' versus the 'raise'? im not totally sure when to use either. i get them mixed up.

1 Answer

Steven Parker
Steven Parker
243,266 Points

The "raise" statement creates an exception, and the "except" statement is what responds when one is created.

One way to think about it is "raise" raises a flag, and "except" is the guy watching for it who does something when he sees it.

I'm sorry but I still don't understand. I still don't really see the difference between the two. I get that "except" is, what on alert in a sense? But can it be used without ever writing the raise part? And vice versa?

Steven Parker
Steven Parker
243,266 Points

Think of "raise" as a "sender" and "except" as the "receiver".

Certain error situations do a "raise" automatically, so an "except" can be useful for handling those even if you don't program "raise" yourself.