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

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.