Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Functions and Looping Expecting Exceptions

Philip Schultz
Philip Schultz
11,413 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
218,654 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,413 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.