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 Exception Flow

Why does the result not include B or E?

spoiler quiz question for exception handling. I ran the code in the workspace after i got it wrong but can someone walk me through this. I am not understanding.

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

2 Answers

print("A")
try:
  result = "test" + 5 # let's call foo
  print("B") # let's call this bar
except ValueError:
  print("C") # if foo OR bar throw a value error then this line will run 
except TypeError:
  print("D") # if foo OR bar throw a type error then this line will run
else:
  print("E") # if foo AND bar run successfully, this line will run - in other words, only if the try doesn't throw an exception.
  print("F") # if foo AND bar run successfully, this line will run - in other words, only if the try doesn't throw an exception.

If foo throws an error, then bar will never be executed because the code flow will either: jump to the except block or throw an exception if the except block is not there to catch it.

To answer your question, B will never run because the program "blew up" while executing the line above (foo). You can't add a number to a string, and that error is a TypeError because it was expecting the int 5 to be of type str "5". The E and F won't run, because the code inside the try did not execute with success.

Try to edit foo to

result = "test" + 5

and see the results.

thank you! The lightbulb just clicked. :)