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

Philip Schultz
Philip Schultz
11,437 Points

Try except block question

Hello, I'm confused as to what fixes the 'name error' in this video. It appears all he did was add an else statement and it fixed the issues.....why is this? If we make a try block that is made for a 'Value Error' it will only stop value errors, unless we add an else statement then it will protect from all possible errors? If this is the case then why are we explicitly saying 'value error' after except when it actually protects us from all errors? Does it protect us from all errors?

1 Answer

Steven Parker
Steven Parker
231,275 Points

The "else" code only runs when there is no error, it does not add extra protection.

This is demonstrated after the "else" code is added when he enters "0" for the value, and still gets an error that is not caught by the "except".

Philip Schultz
Philip Schultz
11,437 Points

Hey Steven, Got it. The else block didn't necessarily catch the NameError.....it prevented it from ever happening. Looks obvious now. Thank you for help again.