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

Mark Roudebush
Mark Roudebush
901 Points

A question about 2 of 2 in the Create a Function quiz

## QUESTION: why doesn't the "B" print? I understand that there was no ValueError so C didn't print and because there was a TypeError "D" printed which means the else ("E") did not. I'm not sure why "B" got skipped. 

print("A")
try:
    result = "test" + 5
    print("B")
except ValueError:
    print("C")
except TypeError:
    print("D")
else:
    print("E")
print("F")

3 Answers

Nope. The error occurs at

result = "test" + 5

so that's the last line of the try that will be executed.

You can run the code for yourself here https://repl.it/repls/UnwillingSpringgreenBrace

Mark Roudebush
Mark Roudebush
901 Points

Ah, that makes sense now, thank you!

The try block will exit out on the line where the TypeError occurs.

More formally:

If an error is encountered, a try block code execution is stopped and transferred down to the relevant except block.

Let me know if this helps here

Mark Roudebush
Mark Roudebush
901 Points

Thanks adam n , but wouldn't it read the print("B") before getting to the try block?

This helped me, too! Thanks.